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 7 1244
vertigo wrote:
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:
>>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.
Thanx
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
vertigo wrote:
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
vertigo wrote:
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>
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
vertigo wrote:
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:
>>words = {"help":20, "copyright":25, "credits":35}
# show dictionary
>>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.
>>for w, s in sorted(words.iteritems()): print w, s
....
copyright 25
credits 35
help 20
--
Juho Schultz
On Sun, 17 Dec 2006 17:00:46 +0100, Juho Schultz <ju**********@pp.inet.fi
wrote:
vertigo wrote:
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:
>>>words = {"help":20, "copyright":25, "credits":35}
# show dictionary
>>>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.
>>>for w, s in sorted(words.iteritems()): print w, s
...
copyright 25
credits 35
help 20
Thanx, it's working :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: bissatch |
last post by:
Hi,
Is it possible to change the class style of an HTML element using
DHTML? For example...
<td class="my_class">Text</td>
I have used DHTML to change style elements such as backgroundColor...
|
by: Wayne |
last post by:
I'm using the following code from the Access Web to open a folder. The
folder opens in "List" View. Is there an addition that I can make to
the code to force the folder to open in "Details" view?...
|
by: BerkshireGuy |
last post by:
I have the following Access code that exports to Excel, inserts a
title, changes the color of the title, and then changes the format of
several columns to currency.
The following code sometimes...
|
by: Altman |
last post by:
I have only done a little programming in C++ so I am still learning but I am
having a problem with a variable that is changing on me. I have tried this
2 ways with the same result. I have a...
|
by: VB Programmer |
last post by:
I have a simple datagrid on a webform. (I'm using VB.NET.)
In the page load I get some data, set the .DataSource property of the dg,
then do a .DataBind. The columns are automatically created.
...
|
by: Scot |
last post by:
I have a class library with two subs in a class, summarized below:
Class
Public num As Integer = 0
Sub 1
num = 1
End Sub
|
by: Blaxer |
last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be...
function Calculate_Something(ByVal...
|
by: David Coffin |
last post by:
I'd like to subclass int to support list access, treating the integer
as if it were a list of bits.
Assigning bits to particular indices involves changing the value of
the integer itself, but...
|
by: andreas |
last post by:
When I copy a vb.net project using date formats from one PC with a windows
date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I
get errors.
How can I change for a while in the...
|
by: samonline |
last post by:
Dear friends,
I have written a little program to read the source of a web page into a Rich Text Box. Now I want to find a specific integer value in that text box and take it into a variable. That...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |