Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 17th, 2006, 01:45 PM
vertigo
Guest
 
Posts: n/a
Default Changing variable to integer


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 ?

Thanx
  #2  
Old December 17th, 2006, 02:05 PM
Dustan
Guest
 
Posts: n/a
Default Re: Changing variable to integer


vertigo wrote:
Quote:
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 ?
Perhaps you meant something more along the lines of this:
Quote:
Quote:
Quote:
>>def PrintWordCountFloat(words):
number = 0
for index, word in enumerate(words):
print "%s %f" % (index, word)
number = number + 1
print "Total words: %d" %(number)
Quote:
Quote:
Quote:
>>PrintWordCountFloat(range(10))
0 0.000000
1 1.000000
2 2.000000
3 3.000000
4 4.000000
5 5.000000
6 6.000000
7 7.000000
8 8.000000
9 9.000000
Total words: 10

Or similar; I can't read your mind. Just know that enumerate(iterable)
yields (index, value) for each item in iterable.
Quote:
Thanx
  #3  
Old December 17th, 2006, 02:05 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: Changing variable to integer

vertigo schrieb:
Quote:
>
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
  #4  
Old December 17th, 2006, 02:05 PM
Peter Otten
Guest
 
Posts: n/a
Default Re: Changing variable to integer

vertigo wrote:
Quote:
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 ?
You could be calling PrintWordCountFloat() twice, once with a dictionary and
then with a list :-)

If I'm wrong, you have to provide more code.

Peter
  #5  
Old December 17th, 2006, 02:25 PM
Fredrik Lundh
Guest
 
Posts: n/a
Default Re: Changing variable to integer

vertigo wrote:
Quote:
I receive such error:
File "p4.py", line 24, in PrintWordCountFloat
print "%s %f" % (word,words[word])
TypeError: list indices must be integers
please post the *entire* traceback message. see:

http://effbot.org/pyfaq/tutor-i-need...at-should-i-do

</F>

  #6  
Old December 17th, 2006, 02:35 PM
vertigo
Guest
 
Posts: n/a
Default Re: Changing variable to integer

Quote:
Perhaps you meant something more along the lines of this:
>
Quote:
Quote:
>>>def PrintWordCountFloat(words):
number = 0
for index, word in enumerate(words):
print "%s %f" % (index, word)
number = number + 1
print "Total words: %d" %(number)
Quote:
Quote:
>>>PrintWordCountFloat(range(10))
0 0.000000
1 1.000000
2 2.000000
3 3.000000
4 4.000000
5 5.000000
6 6.000000
7 7.000000
8 8.000000
9 9.000000
Total words: 10
>
Or similar; I can't read your mind. Just know that enumerate(iterable)
yields (index, value) for each item in iterable.

sorry, i was not precise. words is a dictionary.
1. How can i show it's all variable (with key and value) ?
2. How can i show sorted dictionary (by key) ?
3. Is there any function which could do fast iteration on elements of
sorted dictionary ?

Thanx
  #7  
Old December 17th, 2006, 04:05 PM
Juho Schultz
Guest
 
Posts: n/a
Default Re: Changing variable to integer

vertigo wrote:
Quote:
Quote:
Perhaps you meant something more along the lines of this:
Quote:
>>def PrintWordCountFloat(words):
number = 0
for index, word in enumerate(words):
print "%s %f" % (index, word)
number = number + 1
print "Total words: %d" %(number)
Quote:
>>PrintWordCountFloat(range(10))
0 0.000000
1 1.000000
2 2.000000
3 3.000000
4 4.000000
5 5.000000
6 6.000000
7 7.000000
8 8.000000
9 9.000000
Total words: 10

Or similar; I can't read your mind. Just know that enumerate(iterable)
yields (index, value) for each item in iterable.
>
>
sorry, i was not precise. words is a dictionary.
1. How can i show it's all variable (with key and value) ?
2. How can i show sorted dictionary (by key) ?
3. Is there any function which could do fast iteration on elements of
sorted dictionary ?
>
Thanx
I hope this helps a bit:
Quote:
Quote:
Quote:
>>words = {"help":20, "copyright":25, "credits":35}
# show dictionary
Quote:
Quote:
Quote:
>>for w, s in words.iteritems(): print w, s
....
credits 35
help 20
copyright 25
# show sorted dictionary
# dicts are not ordered, so you have to sort them.
Quote:
Quote:
Quote:
>>for w, s in sorted(words.iteritems()): print w, s
....
copyright 25
credits 35
help 20

--
Juho Schultz

  #8  
Old December 17th, 2006, 05:25 PM
vertigo
Guest
 
Posts: n/a
Default Re: Changing variable to integer

On Sun, 17 Dec 2006 17:00:46 +0100, Juho Schultz <juho.schultz@pp.inet.fi
wrote:
Quote:
vertigo wrote:
Quote:
Quote:
Perhaps you meant something more along the lines of this:
>
>>>def PrintWordCountFloat(words):
number = 0
for index, word in enumerate(words):
print "%s %f" % (index, word)
number = number + 1
print "Total words: %d" %(number)
>>>PrintWordCountFloat(range(10))
0 0.000000
1 1.000000
2 2.000000
3 3.000000
4 4.000000
5 5.000000
6 6.000000
7 7.000000
8 8.000000
9 9.000000
Total words: 10
>
Or similar; I can't read your mind. Just know that enumerate(iterable)
yields (index, value) for each item in iterable.
>>
>>
>sorry, i was not precise. words is a dictionary.
>1. How can i show it's all variable (with key and value) ?
>2. How can i show sorted dictionary (by key) ?
>3. Is there any function which could do fast iteration on elements of
>sorted dictionary ?
>>
>Thanx
>
I hope this helps a bit:
>
Quote:
Quote:
>>>words = {"help":20, "copyright":25, "credits":35}
# show dictionary
Quote:
Quote:
>>>for w, s in words.iteritems(): print w, s
...
credits 35
help 20
copyright 25
# show sorted dictionary
# dicts are not ordered, so you have to sort them.
Quote:
Quote:
>>>for w, s in sorted(words.iteritems()): print w, s
...
copyright 25
credits 35
help 20
>
Thanx, it's working :)
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles