473,779 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dictionaries again - where do I make a mistake?

Lad
I use the following code to sort dictionary.
Olddict={'r':4, 'c':1,'d':2,'e' :3,'f':2}
Newdict={}
i = [(val, key) for (key, val) in Olddict.items()]
i.sort() # by val
i.reverse() # Get largest first.
for (val, key) in i:
print key,val
Newdict[key]=val
print Olddict
print Newdict
Sorting seems to be OK,.
the command
print key,val
prints the proper values
but I can not create Newdict to be sorted properly.

Where do I make a mistake?
Thank you for help.
L

Oct 19 '06 #1
2 1151
Lad wrote:
Sorting seems to be OK,.
the command
print key,val
prints the proper values
but I can not create Newdict to be sorted properly.

Where do I make a mistake?
Thank you for help.
Dictionaries are unordered -- the order in which items come out is
unspecified. It's based on the details of their internal storage mechanism (a
hash table), and you can't control it at all.

If you need your pairs in a certain order, you'll have to use a list of tuples.

Dustin
Oct 19 '06 #2
but I can not create Newdict to be sorted properly.
>
Where do I make a mistake?
By assuming that dictionaries *can* be sorted.

For more reading:
http://aspn.activestate.com/ASPN/Pyt...k/Recipe/52306
http://aspn.activestate.com/ASPN/Coo.../Recipe/107747

They're intrinsically an unsorted datatype.

-tkc

Oct 19 '06 #3

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

Similar topics

7
2199
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for x in range(10):
8
2623
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 = {}
6
1346
by: mercuryprey | last post by:
Hello, I want to do the following: def do_load(self, arg): sitefile = file('sitelist', 'r+', 1) while True: siteline = sitefile.readline() site_rawlist = siteline.split() sitelist] = site_rawlist if len(siteline) == 0:
1
1177
by: Talin | last post by:
Thanks to everyone for pointing out my bone-headed errors :) I don't *usually* make that sort of mistake... What I'm attempting to do is create an implentation of the unification algorithm. Now, its fairly straightforward to do unification on my own custom types, all I have to do is define a unify() method for each class: def unify( self, other, bindings ): ...
210
10556
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again I am missing that feature. Maybe there is something wrong with my programming style, but I rather think it is generally useful. I fully agree with the following posting where somebody complains why so very basic and useful things are not part...
5
4087
by: Livin | last post by:
I'm a noobie so please be easy on me. I have searched a ton and did not find anything I could understand. I'm using py2.3 I've been using Try/Except but this gets long with multiple dictionaries. I found some code on web pages and such but cannot get them to work. Any help is appreciated as I need to search multiple dictionaries for keys.
5
1498
by: Ladislav Andel | last post by:
Hi, I have a list of dictionaries. e.g. how could I make a new list of dictionaries which would look like: , 'service_domain': 'dp0.example.com'}, {'transports': , 'service_domain': 'dp1.example.com'}] Could you help me, please?
1
165
by: Edwin.Madari | last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar = 0 and re-run.... fun learning with python... Edwin -----Original Message----- From: Madari, Edwin Sent: Thursday, August 14, 2008 9:24 PM
14
1824
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.