提问人:akbiggs 提问时间:10/22/2010 更新时间:10/23/2010 访问量:3967
如何检查另一个字符串中是否存在一个确切的字符串?
How do I check for if an exact string exists in another string?
问:
我目前遇到了一些问题。我正在尝试编写一个程序,该程序将突出显示另一个字符串中出现的单词或短语,但前提是它所匹配的字符串完全相同。我遇到麻烦的部分是确定我与该短语匹配的子短语是否包含在另一个更大的子短语中。
显示此问题的快速示例:
>>> indicators = ["therefore", "for", "since"]
>>> phrase = "... therefore, I conclude I am awesome."
>>> indicators_in_phrase = [indicator for indicator in indicators
if indicator in phrase.lower()]
>>> print indicators_in_phrase
['therefore', 'for']
我不希望将“for”包含在该列表中。我知道为什么包含它,但我想不出任何表达式可以过滤掉这样的子字符串。
我注意到网站上还有其他类似的问题,但每个问题都涉及正则表达式解决方案,这是我还不舒服的事情,尤其是在 Python 中。有没有一种简单的方法可以在不使用正则表达式的情况下解决这个问题?如果没有,相应的正则表达式以及如何在上面的示例中实现它将不胜感激。
答:
有一些方法可以在没有正则表达式的情况下做到这一点,但这些方法中的大多数都非常复杂,以至于您希望自己花时间学习所需的简单正则表达式序列。
评论
有点长,但给出了一个想法/当然,正则表达式是为了让它变得简单
>>> indicators = ["therefore", "for", "since"]
>>> phrase = "... therefore, I conclude I am awesome."
>>> phrase_list = phrase.split()
>>> phrase_list
['...', 'therefore,', 'I', 'conclude', 'I', 'am', 'awesome.']
>>> phrase_list = [ k.rstrip(',') for k in phrase_list]
>>> indicators_in_phrase = [indicator for indicator in indicators if indicator in phrase_list]
>>> indicators_in_phrase
['therefore']
我认为你要做的更像是这样的事情:
import string
words_in_phrase = string.split(phrase)
现在,您将在如下列表中显示这些单词:
['...', 'therefore,', 'I', 'conclude', 'I', 'am', 'awesome.']
然后像这样比较列表:
indicators_in_phrase = []
for word in words_in_phrase:
if word in indicators:
indicators_in_phrase.append(word)
可能有几种方法可以使它不那么冗长,但我更喜欢清晰。此外,您可能不得不考虑删除标点符号,如“awesome.”和“因此”。
为此,请使用 rstrip 作为另一个答案中的答案
“for”的问题在于它在“因此”里面,还是它不是一个词?例如,如果你的一个指标是“敬畏”,你是否希望它被包含在indicators_in_phrase中?
您希望如何处理以下情况? 指标 = [“abc”, “cde”] phrase = “一 abcde 二”
评论
您可以从短语中去除标点符号,然后拆分它,以便所有单词都是单独的。然后你可以做你的字符串比较
>>> indicators = ["therefore", "for", "since"]
>>> phrase = "... therefore, I conclude I am awesome."
>>> ''.join([ i for i in phrase.lower() if i not in string.punctuation]).strip().split()
['therefore', 'I', 'conclude', 'I', 'am', 'awesome']
>>> p = ''.join([ i for i in phrase.lower() if i not in string.punctuation]).strip().split()
>>> indicators_in_phrase = [indicator for indicator in indicators if indicator in p ]
>>> indicators_in_phrase
['therefore']
这是一行正则表达式...
import re
indicators = ["therefore", "for", "since"]
phrase = "... therefore, I conclude I am awesome."
indicators_in_phrase = set(re.findall(r'\b(%s)\b' % '|'.join(indicators), phrase.lower()))
评论
\b(therefore|for|since)\b
\b
- 创建一组指标
- 创建短语集
- 查找交叉点
法典:
indicators = ["therefore", "for", "since"]
phrase = "... therefore, I conclude I am awesome."
print list(set(indicators).intersection(set( [ each.strip('.,') for each in phrase.split(' ')])))
干杯:)
评论
each.strip('.').strip(',')
each.strip('.,')
正则表达式是最简单的方法! 提示:
re.compile(r'\btherefore\b')
然后你可以改变中间的单词!
编辑:我为你写了这个:
import re
indicators = ["therefore", "for", "since"]
phrase = "... therefore, I conclude I am awesome. "
def find(phrase, indicators):
def _match(i):
return re.compile(r'\b%s\b' % (i)).search(phrase)
return [ind for ind in indicators if _match(ind)]
>>> find(phrase, indicators)
['therefore']
评论