Changing variable to integer

V

vertigo

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
 
D

Dustan

vertigo said:
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:
number = 0
for index, word in enumerate(words):
print "%s %f" % (index, word)
number = number + 1
print "Total words: %d" %(number)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.
 
D

Diez B. Roggisch

vertigo said:
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
 
P

Peter Otten

vertigo said:
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
 
V

vertigo

Perhaps you meant something more along the lines of this:

number = 0
for index, word in enumerate(words):
print "%s %f" % (index, word)
number = number + 1
print "Total words: %d" %(number)
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
 
J

Juho Schultz

vertigo said:
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:
....
credits 35
help 20
copyright 25
# show sorted dictionary
# dicts are not ordered, so you have to sort them.....
copyright 25
credits 35
help 20
 
V

vertigo

I hope this helps a bit:

...
credits 35
help 20
copyright 25
# show sorted dictionary
# dicts are not ordered, so you have to sort them.
...
copyright 25
credits 35
help 20

Thanx, it's working :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top