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

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

Reply via email to