【发布时间】:2012-11-23 18:53:02
【问题描述】:
如果我有一个字符串并想返回一个包含空格的单词,该怎么做?
例如,我有:
line = 'This is a group of words that include #this and @that but not ME ME'
response = [ word for word in line.split() if word.startswith("#") or word.startswith('@') or word.startswith('ME ')]
print response ['#this', '@that', 'ME']
所以 ME ME 因为空白而没有被打印出来。
谢谢
【问题讨论】:
-
你实际想要的输出是什么?
-
程序如何知道 ME ME 是一个包含空格的单词而不是两个单词? (一个单词怎么可能包含空格?)
-
ME ME 不是一个词,而是一个前缀(ME),然后是空格,然后是词(ME)。我希望程序能够全部完成。抱歉没有更清楚。
标签: python regex python-2.7