How to match exact string/word while searching a list. I have tried, but its not correct. below I have given the sample list, my code and the test results
list = ['Hi, friend', 'can you help me?']
my code
dic=dict()
for item in list:
for word in item.split():
dic.setdefault(word, list()).append(item)
print dic.get(s)
test results:
s = "can" ~ expected output: 'can you help me?' ~ output I get: 'can you help me?'
s = "you" ~ expected output: *nothing* ~ output I get: 'can you help me?'
s = "Hi," ~ expected output: 'Hi, friend' ~ output I get: 'Hi, friend'
s = "friend" ~ expected output: *nothing* ~ output I get: 'Hi, friend'
My list contains 1500 strings. Anybody can help me??
Looks like you need a map of sentences and their starting word, so you don’t need to map all words in that sentence but only the first one.
output:
Also note few things from the code above
listas name of variable because python uses it forlist class