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
months = { '@@@@@@@@@@':0, '':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) )
==============

please tell me Uli why this dont work as expected to.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-10-2013
On Mon, 10 Jun 2013 03:42:38 -0700, Νικόλαος Κούρας wrote:

> for key in sorted( months.values() ):


> please tell me Uli why this dont work as expected to.


Because values are not keys. You are looking at the values, and trying to
use them as keys.

months = {'Φεβρουάριος':2, 'Ιανουάριος':1}
print("==Values==")
for x in sorted(months.values()):
print(x)

print("==Keys==")
for x in sorted(months.keys()):
print(x)


prints:


==Values==
1
2
==Keys==
Ιανουάριος
Φεβρουάριος



--
Steven
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      06-10-2013
Am 10.06.2013 11:48, schrieb Νικόλαος Κούρας:
> 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]) )


This code is still sending a misleading message. What you are referring
to as "key" here is in fact a (key, value) tuple. I'd use Fábio's
suggestion and use the automatic splitting:

for name, idx in sorted(months.items(), key=lambda num : num[1]):
print('month #{} is {}'.format(idx, name))


> 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.


You are composing three things:

1. months.values() - gives you a sequence with the month numbers
2. sorted() - gives you a sorted sequence
3. for-iteration - iterates over a sequence

At which point is Python doing anything non-obvious? Also, have you
considered reversing the dictionary mapping or creating a second one
with the reversed mapping? Or maybe take a look at collections.OrderedDict?


> names set() was able to order like this why not the dictionary too?


Well, why don't you use a set then, if it solves your problem? An in
which place does anything behave differently? Sorry to bring you the
news, but your expectations are not fulfilled because your assumptions
about how things should work are already flawed, I'm afraid.


Uli

 
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