gpt4 book ai didi

python - 在字符串列表中查找完全匹配

转载 作者:太空狗 更新时间:2023-10-30 00:24:08 26 4
gpt4 key购买 nike

对此很陌生,所以请多多包涵...

我有一个预定义的单词列表

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com