473,385 Members | 1,693 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,385 software developers and data experts.

How to sort using hash's key?

Hello,

I want to sort a dict via its key,but I have no idea on how to do it.
Please help me,thanks.
---------------------------------------------------------------------------
3webXS HiSpeed Dial-up...surf up to 5x faster than regular dial-up alone...
just $14.90/mo...visit www.get3web.com for details

Jan 31 '07 #1
3 1823
JoJo wrote:
I want to sort a dict via its key,but I have no idea on how to do it.
>>d = dict(a=2, b=1)
for key in sorted(d):
.... print key, d[key]
....
a 2
b 1

STeVe
Feb 1 '07 #2
Steven Bethard <st************@gmail.comwrites:
JoJo wrote:
I want to sort a dict via its key,but I have no idea on how to do
it.
>d = dict(a=2, b=1)
for key in sorted(d):
... print key, d[key]
...
a 2
b 1
That's not a solution to "sort the dict"; that's getting a particular
representation of the dict. It's likely the original poster wants the
equivalent to list.sort() instead of a one-time output of the
contents.

--
\ "I installed a skylight in my apartment. The people who live |
`\ above me are furious!" -- Steven Wright |
_o__) |
Ben Finney

Feb 1 '07 #3
Ben Finney wrote:
Steven Bethard <st************@gmail.comwrites:
>JoJo wrote:
>>I want to sort a dict via its key,but I have no idea on how to do
it.
d = dict(a=2, b=1)
for key in sorted(d):
... print key, d[key]
...
a 2
b 1

That's not a solution to "sort the dict"; that's getting a particular
representation of the dict. It's likely the original poster wants the
equivalent to list.sort() instead of a one-time output of the
contents.
Yes, but as others have pointed out, you can't get that with a standard
Python dict. 99% of the time when a poster asks about how to sort a
dict, their problem can be solved by simply sorting the dict keys or values.

STeVe
Feb 1 '07 #4

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

Similar topics

2
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this?...
2
by: Tommo | last post by:
Hello All, I have a slight problem that goes like this. I have created a Perl hash where the keys are made up of numeric values, I was then sorting the 'keys' for this hash on their value (a<=>b...
3
by: Casey | last post by:
Hi, I haven't being using perl for too long. Can someone explain the correct way to get the sort function to recognize an anonymous function declared as a hash value? Look at my sample code for...
1
by: gerrod | last post by:
Hi - Does anyone know a way to created a SortedList (in the System.Collections namespace) that will sort on VALUES instead of KEYS... ? The scenario is this - I have a SortedList containing...
3
by: Alexander Widera | last post by:
Hi, I have a problem with this code ... (see below) ... I want to sort an instance of MyList ... by MyData.Shortname ... Shortname is of the type string.... how can I sort the entries? Thank...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: julian_m | last post by:
Let's say I've a page which is called with arguments a, b and c test.php?a=val_a&b=val_b&c=val_c but, i don't want to allow anyone to modify either val_a, val_b or val_c I thought it would...
23
by: Brian Blais | last post by:
Hello, I have two lists, one with strings (filenames, actually), and one with a real-number rank, like: A= B= I'd like to sort list A using the values from B, so the result would be in...
1
by: RC | last post by:
I have simple hashtable object function myHashTable(key, value) { this.key = key; this.value = value; } var myObjectArray = new Array(5); myObjectArray = new myHashTable(1, "2006-06-07");
0
by: Ben Finney | last post by:
JoJo <Jstone@3web.comwrites: The 'dict' type is explicitly unordered. Sorting it isn't possible. (Also note that a 'hash' is something entirely different in Python.) You can create your own...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.