如何在搜索列表時匹配精確的字符串/詞。我試過了,但它不正確。下面我給了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串
正則表達式? – Gandi