Changing variable to integer
Diez B. Roggisch
deets at nospam.web.de
Sun Dec 17 08:59:42 EST 2006
vertigo schrieb:
>
> Hello
>
> I receive such error:
> File "p4.py", line 24, in PrintWordCountFloat
> print "%s %f" % (word,words[word])
> TypeError: list indices must be integers
>
> i call PrintWordCountFloat with hash table, keys are words(string) and
> values float.
> This part of the code:
>
> def PrintWordCountFloat(words):
> number = 0
> for word in words:
> print "%s %f" % (word,words[word]) #line 24
> number = number + 1
> print "Total words: %d" %(number)
>
> My function displays whole table correctly, and after that i receive
> mentioned error.
> Why ? Where is the problem ?
words is a list. If your variable names mean anything, I presume that
word is ... well, a word, thus a string. But you can't do
[1,2,4]['some_word']
That only works on dictionaries, like this:
{'some_word' : 100}['some_word']
Diez
More information about the Python-list
mailing list