[Tutor] Sorting Dictionary Keys

2008-05-13 Thread FT
ist[j], k,i) j+=1 --- On Tue, 5/13/08, James Hartley <[EMAIL PROTECTED]> wrote: From: James Hartley <[EMAIL PROTECTED]> Subject: [Tutor] sorting dictionary keys? To: tutor@python.org Date: Tuesday, May 13, 2008, 11:06 AM I suspect this is a brain-dead question... Give

Re: [Tutor] sorting dictionary keys?

2008-05-13 Thread Kent Johnson
On Tue, May 13, 2008 at 7:58 AM, Norman Khine <[EMAIL PROTECTED]> wrote: > how about this > > > >>> d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 } > >>> for i in sorted(set(d)): > ... print "%s\t%s" % (i, d[i]) The set() is not needed. Also to iterate over key, value pairs in order by key you

Re: [Tutor] sorting dictionary keys?

2008-05-13 Thread Kent Johnson
On Tue, May 13, 2008 at 7:32 AM, Mugund K <[EMAIL PROTECTED]> wrote: > > A quick but ugly [brain-dead :-) ]fix would be sorting keys seperately, > >>> temp = d.keys() > >>> temp.sort() > >>> for i in temp: Not so ugly; before sorted() was introduced (Python 2.4) that would be the way to do it. K

Re: [Tutor] sorting dictionary keys?

2008-05-13 Thread Norman Khine
how about this >>> d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 } >>> for i in sorted(set(d)): ... print "%s\t%s" % (i, d[i]) ... a 1 b 3 c 0 d 2 James Hartley wrote: I suspect this is a brain-dead question... Given the following code, output is as expected: $ cat te

Re: [Tutor] sorting dictionary keys?

2008-05-13 Thread Kent Johnson
On Tue, May 13, 2008 at 7:06 AM, James Hartley <[EMAIL PROTECTED]> wrote: > But if the keys are sorted, I get an error: > $ cat test1.py > d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 } > > for i in d.keys().sort(): > print "%s\t%s" % (i, d[i]) > $ python test1.py > Traceback (most recent ca

Re: [Tutor] sorting dictionary keys?

2008-05-13 Thread Mugund K
t;>   Thnx, Mugund--- On Tue, 5/13/08, James Hartley <[EMAIL PROTECTED]> wrote: From: James Hartley <[EMAIL PROTECTED]>Subject: [Tutor] sorting dictionary keys?To: tutor@python.orgDate: Tuesday, May 13, 2008, 11:06 AMI suspect this is a brain-dead question... Given the following co

[Tutor] sorting dictionary keys?

2008-05-13 Thread James Hartley
I suspect this is a brain-dead question... Given the following code, output is as expected: $ cat test.py d = { 'a' : 1, 'd' : 2, 'b' : 3, 'c' : 0 } for i in d.keys(): print "%s\t%s" % (i, d[i]) $ python test.py a 1 c 0 b 3 d 2 $ But if the keys are sorted, I get an