[Tutor] matching words from a text to keys in a dictionary
Andre Engels
andreengels at gmail.com
Mon Mar 8 17:26:46 CET 2010
On Mon, Mar 8, 2010 at 5:05 PM, Karjer Jdfjdf <karper12345 at yahoo.com> wrote:
> I want to compare words in a text to a dictionary with values attached to
> the words.
>
> The dictionary looks like:
> { word1: [1,2,3] word2: [2,3,4,a,b ] ... }
>
Please give the actual dictionary, not something that it 'looks like' - an
actual dictionary would never 'look like' this: it has commas between the
elements, and quotes around anything that is a word.
> I'm trying to find a way to achieve this, but I'm having trouble getting
> corrects results.
>
> If I do the def below, nothing is matched.
>
> def searchWord(text, dictionary):
> text = text.split()
> for word in text:
> print word
> if word in dictionary:
> value = dictionary[str(word)]
> else:
> value = None
> return w
>
> If I try another way, I keep getting errors:
>
> def searchWord(text, dictionary):
> for key in dictionary:
> value = dictionary[key]
> if re.search(key, text):
> w = value
> else:
> w = None
> return w
>
>
> TypeError: list indices must be integers, not str
>
That's quite a clear statement: If this is indeed caused by the function you
show here, then the only explanation is that 'dictionary' is not a
dictionary at all, but a list.
--
André Engels, andreengels at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100308/5cfe0201/attachment.html>
More information about the Tutor
mailing list