Re: How to sort using hash's key?

2007-01-31 Thread Steven Bethard
Ben Finney wrote:
> Steven Bethard <[EMAIL PROTECTED]> writes:
> 
>> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to sort using hash's key?

2007-01-31 Thread Ben Finney
Steven Bethard <[EMAIL PROTECTED]> writes:

> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to sort using hash's key?

2007-01-31 Thread Steven Bethard
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
-- 
http://mail.python.org/mailman/listinfo/python-list

Sorting a dict (was: How to sort using hash's key?)

2007-01-31 Thread Ben Finney
JoJo <[EMAIL PROTECTED]> writes:

> I want to sort a dict via its key,but I have no idea on how to do
> it.

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 type, inheriting from 'dict', that has the
behaviour you want. See the Python Cookbook for some examples.

http://aspn.activestate.com/ASPN/Python/Cookbook/>

-- 
 \ "I cannot conceive that anybody will require multiplications at |
  `\   the rate of 40,000 or even 4,000 per hour ..."  -- F. H. Wales, |
_o__) 1936 |
Ben Finney

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to sort using hash's key?

2007-01-31 Thread Cliff Wells
On Thu, 2007-02-01 at 04:54 +0800, JoJo wrote:

> I want to sort a dict via its key,but I have no idea on how to do it.
> Please help me,thanks.

You can't.  There is, however, a recipe for an "ordered dict" on ASPN:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/438823


> 3webXS HiSpeed Dial-up...surf up to 5x faster than regular dial-up alone... 
> just $14.90/mo...visit www.get3web.com for details

You pay $15/mo for dialup???

Regards,
Cliff

-- 
http://mail.python.org/mailman/listinfo/python-list


How to sort using hash's key?

2007-01-31 Thread JoJo
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

-- 
http://mail.python.org/mailman/listinfo/python-list