Anoop
Anoop

Reputation: 1435

match only exact word/string in python

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??

Upvotes: 0

Views: 1384

Answers (2)

Abhijit
Abhijit

Reputation: 63727

In case if you just want to see if the sentence starts with a given words you can try startswith if you don;t want the searched word to be at word boundary or split()[0] if you want it to match at word boundary. As an example

>>> def foo(s): # @ word boundary
    return [x for x in l if x.split()[0]==s]

>>> def bar(s): # Prefix
    return [x for x in l if x.startswith(s)]

Also refrain from overlaying python global name-space like what you did when you named your list as list. I have called it l in my example.

Upvotes: 1

Anurag Uniyal
Anurag Uniyal

Reputation: 88747

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.

from collections import defaultdict

sentences = ['Hi, friend', 'can you help me?']

start_sentence_map = defaultdict(list)
for sentence in sentences:
    start = sentence.split()[0]
    start_sentence_map[start].append(sentence)

for s in ["can", "you", "Hi,", "friend"]:
    print s,":",start_sentence_map.get(s)

output:

can : ['can you help me?']
you : None
Hi, : ['Hi, friend']
friend : None

Also note few things from the code above

  1. Don't use name list as name of variable because python uses it for list class
  2. Use default dict which makes it easy to directly add entries to dictionary instead of first adding a default entry
  3. Better descriptive names instead of mylist, or dic

Upvotes: 1

Related Questions