<p>所以我有:</p>
<pre><code>text = 'Hi my name is !bob i like to !eat'
</code></pre>
<p>我想创建一个包含感叹号后面的单词的列表。所以我想要一个函数,它创建一个如下的结果:</p>
<pre><code>>>> my_function(text)
['bob', 'eat']
</code></pre>
<p>到目前为止,我能想到的只有:</p>
<pre><code>>>> test_string = text['!':' ']
</code></pre>
<p>但我不能用字符串代替索引。有什么建议吗</p>
<p>注:我希望他们都是小写与副本包括在内</p>
<p>最简单的解决方案[可能看起来不像pythonic;)]我能想到的是:</p>
<pre><code>words = [x[1:] for x in text.split(" ") if x[0]=='!']
print(words)
</code></pre>
<p>输出:</p>
<pre><code>['bob', 'eat']
</code></pre>