Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Sorting a set works, sorting a dictionary fails ?

Reply
Thread Tools

Sorting a set works, sorting a dictionary fails ?

 
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-10-2013
sets and dicts are unordered.

================
Yo order the a set i use:
names = set() #the i fill it with data

for name in sorted( names ):
================

Now for the dictionary:

================
months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }

for key in sorted( months.keys() ):
================

I'm having trouble ordering a dictionary though.

But how come i was able to sort the set names() and not being able to sort the dictionary keys as well with the sorted function?= i used?
 
Reply With Quote
 
 
 
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-10-2013
What if i wanted to sort it out if alphabetically and not by the values?

Thsi worked:

for item in sorted(months.items(),key=lambda num : num[1]):

but this failed:

for item in sorted(months.items()):

why?
 
Reply With Quote
 
 
 
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-10-2013
Τη Δευτέρα, 10 Ιουνίου 2013 11:16:37 π.μ. UTC+3, ο χρήστης Νικόλαος Κούρας έγραψε:
> What if i wanted to sort it out if alphabetically and not by the values?
>
>
>
> Thsi worked:
>
>
>
> for item in sorted(months.items(),key=lambda num : num[1]):
>
>
>
> but this failed:
>
>
>
> for item in sorted(months.items()):
>
>
>
> why?


sorry what i was tryign to say was why not as: for item in sorted(months.values()):

 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-10-2013
Trying this:

months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }

for key in sorted( months.values() ):
print('''
<option value="%s"> %s </option>
''' % (months[key], key) )


output this:

[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>, referer: http://superhost.gr/
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' % (months[key], key) ), referer: http://superhost.gr/
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1, referer: http://superhost.gr/

KeyError 1 ??!! All i did was to tell python to sort the dictionary values,which are just integers.
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      06-10-2013
Am 10.06.2013 10:04, schrieb Νικόλαος Κούρας:
> months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
> 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
>
> for key in sorted( months.keys() ):
> ================
>
> I'm having trouble ordering a dictionary though.


I can't find a problem here. I tried simple dictionaries containing
numbers as keys using Python 3.3, and sorting the keys works without any
problem there. What exactly is the "trouble" you are having? Be a bit
more precise and describe what you saw and, just in case, also what you
expected to see.

BTW: You have a line continuation there using a backslash. This isn't
necessary, since the pair of {} automatically tell Python the target range.


Good luck!

Uli


 
Reply With Quote
 
Fαbio Santos
Guest
Posts: n/a
 
      06-10-2013
On 10 Jun 2013 09:34, "Νικόλαος Κούρας" <(E-Mail Removed)> wrote:
>
> Trying this:
>
> months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4,

'Μάϊος':5, 'Ιούνιος':6, \
> 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10,

'Νοέμβριος':11, 'Δεκέμβριος':12 }
>
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
>
>
> output this:
>
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] File

"/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>,
referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' %

(months[key], key) ), referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1,

referer: http://superhost.gr/
>
> KeyError 1 ??!! All i did was to tell python to sort the dictionary

values, which are just integers.
> --
> http://mail.python.org/mailman/listinfo/python-list


KeyError: 1 means that there is no int(1) key. I think you meant to do "for
key in sorted(yourdict.keys())"

 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      06-10-2013
Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας:
> for key in sorted( months.values() ):

^^^ ^^^^^^

> KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers.


....and which you then proceed to use as key, which is obviously wrong.

Uli

 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-10-2013
After many tried this did the job:

for key in sorted(months.items(),key=lambda num : num[1]):
print('''
<option value="%s"> %s </option>
''' % (key[1], key[0]) )


but its really frustrating not being able to:

for key in sorted( months.values() ):
print('''
<option value="%s"> %s </option>
''' % (months[key], key) )

Which seemed to be an abivous way to do it.
names set() was able to order like this why not the dictionary too?
 
Reply With Quote
 
Fαbio Santos
Guest
Posts: n/a
 
      06-10-2013
On 10 Jun 2013 10:53, "Νικόλαος Κούρας" <(E-Mail Removed)> wrote:
>
> After many tried this did the job:
>
> for key in sorted(months.items(),key=lambda num : num[1]):
> print('''
> <option value="%s"> %s </option>
> ''' % (key[1], key[0]) )
>
>
> but its really frustrating not being able to:
>
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
>
> Which seemed to be an abivous way to do it.
> names set() was able to order like this why not the dictionary too?


Why not

for key, value in sorted(d.items()):

Tuples are totally sortable.

 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      06-10-2013
Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε:
> Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας:
>
> > for key in sorted( months.values() ):

>
> ^^^ ^^^^^^
>
>
>
> > KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers.

>
>
>
> ...and which you then proceed to use as key, which is obviously wrong.


How hsould have i written it then?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments