对此很陌生,所以请多多包涵...
我有一个预定义的单词列表
checklist = ['A','FOO']
还有一个来自 line.split()
的单词列表,看起来像这样
words = ['fAr', 'near', 'A']
我需要在words
中精确匹配checklist
,所以我只找到'A':
if checklist[0] in words:
那没有用,所以我尝试了在这里找到的一些建议:
if re.search(r'\b'checklist[0]'\b', line):
无济于事,因为我显然无法查找这样的列表对象...对此有任何帮助吗?
最佳答案
使用集合比遍历列表要快得多。
checklist = ['A', 'FOO']
words = ['fAr', 'near', 'A']
matches = set(checklist).intersection(set(words))
print(matches) # {'A'}
关于python - 在字符串列表中查找完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33644729/