473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Handling sorted dictionaries

The following code gives the error

d=sortedmachines[machine]
TypeError: list indices must be integers
What works for the unsorted dictionary does not work for the sorted
dictionary.
Can anyone help?
machinekey = "111111"

machines = {}
machines[machinekey]=[1,0,0,0,0,0,0,0,0,0,0,0,0]

machinekey = "222222"

machines[machinekey]=[0,1,0,0,0,0,0,0,0,0,0,0,0]

ddd=0
for machine in machines.keys():
d=machines[machine]
print machine
print d [ddd]

sortedmachines=sorted(machines)
for machine in sortedmachines:
d=sortedmachines[machine]
print machine
print d [ddd]

Apr 17 '07 #1
2 1565
On Tue, 17 Apr 2007 01:01:55 -0700, loial wrote:
The following code gives the error

d=sortedmachines[machine]
TypeError: list indices must be integers
What works for the unsorted dictionary does not work for the sorted
dictionary.
Can anyone help?
The error message you got tells you what the problem is. sortedmachines
is not a "sorted dictionary". There is no such thing -- dictionaries, also
known as "hash tables" in other languages, are unsorted. sortedmachines is
a _list_, just like the error message says, and the index must be an
integer.

sortedmachines=sorted(machines)
sortedmachines is now the sorted _keys_ copied from machines. Try calling
"print sortedmachines" and looking at what you get.
for machine in sortedmachines:
d=sortedmachines[machine]
Try this instead:

d = machines[machine]
--
Steven D'Aprano

Apr 17 '07 #2
Hello,

Dictionary items are inordered and can not be sorted. Function
sorted() returns list of tuples in each is 0. item key from dictionary
and 1. is value from key-value pair from dict machines. List indices
must be integers and machine keys are strings. See http://www.python.org/dev/peps/pep-0265/
about sorting dictionaries.
You need to know what type is variable in each line of code.
Dict is not same as list, even when you can get values from they very
similar way. dict[key] and list[index], key can be everything what can
be hashed, but index can be only integer.

I am not english, so I hope you understand me.

loial napísal(a):
The following code gives the error
d=sortedmachines[machine]
TypeError: list indices must be integers
What works for the unsorted dictionary does not work for the sorted
dictionary.
Can anyone help?
machinekey = "111111"
machines = {}
machines[machinekey]=[1,0,0,0,0,0,0,0,0,0,0,0,0]
machinekey = "222222"
machines[machinekey]=[0,1,0,0,0,0,0,0,0,0,0,0,0]
ddd=0
for machine in machines.keys():
d=machines[machine]
print machine
print d [ddd]

sortedmachines=sorted(machines)
for machine in sortedmachines:
d=sortedmachines[machine]
print machine
print d [ddd]
Apr 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
12
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of...
13
by: vd12005 | last post by:
Hello, i would like to sort(ed) and reverse(d) the result of many huge dictionaries (a single dictionary will contain ~ 150000 entries). Keys are words, values are count (integer). i'm...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
17
by: Adam Olsen | last post by:
As was seen in another thread, there's a great deal of confusion with regard to surrogates. Most programmers assume Python's unicode type exposes only complete characters. Even CPython's own...
94
by: Chad | last post by:
On to top of page 163 in the book "The C Programming Langauge" by K & R, they have the following: char *strdup(char *s) { char *p; p=(char *)malloc(strlen(s)+1); if( p != NULL) strcpy(p,s):...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.