如何在搜索列表时精确匹配字符串/单词。我试过了,但不正确。下面我给出了sample list
、my code
和test results
list = ['Hi, hello', 'hi mr 12345', 'welcome sir']
我的代码:
for str in list: if s in str: print str
测试结果:
s = "hello" ~ expected output: 'Hi, hello' ~ output I get: 'Hi, hello' s = "123" ~ expected output: *nothing* ~ output I get: 'hi mr 12345' s = "12345" ~ expected output: 'hi mr 12345' ~ output I get: 'hi mr 12345' s = "come" ~ expected output: *nothing* ~ output I get: 'welcome sir' s = "welcome" ~ expected output: 'welcome sir' ~ output I get: 'welcome sir' s = "welcome sir" ~ expected output: 'welcome sir' ~ output I get: 'welcome sir'
我的列表包含超过200K个字符串
如果搜索完全匹配:
看来您不仅需要执行一次此搜索,因此我建议您将列表转换为字典:
所以现在你可以很容易地做到:
p.s.可能需要改进
item.split()
来处理逗号、指针和其他分隔符。可以使用regex和\w
。p.p.s.正如库拉里安所说,这与“欢迎先生”不符。如果要匹配整个字符串,它只是建议的解决方案的另一行。但如果必须匹配字符串中以空格和标点符号为界的部分,则应该选择
regex
。相关问题 更多 >
编程相关推荐