Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47553

Re: Sorting a set works, sorting a dictionary fails ?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!not-for-mail
From Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Newsgroups comp.lang.python
Subject Re: Sorting a set works, sorting a dictionary fails ?
Date Mon, 10 Jun 2013 15:11:58 +0200
Lines 46
Message-ID <utvg8a-q0c.ln1@satorlaser.homedns.org> (permalink)
References <3b252a10-a7e3-40f5-99dc-8afcf72cf997@googlegroups.com> <ce76b1a2-62d0-481d-b045-4bc791a84e37@googlegroups.com> <a609b6e4-b5a5-4d72-b5e1-c7271efe8a72@googlegroups.com> <c2eb322d-ac51-4e6a-aa56-3b6b8267ebc4@googlegroups.com> <mailman.2960.1370856443.3114.python-list@python.org> <7c0e481e-97dd-4d36-808c-88e3580d8311@googlegroups.com> <56f10268-0e26-4f86-ae03-8fd740cc4544@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de vv6Fq6XCu/yy7OYg1w3a1AfqW89eWzNvTPVGUqBgi/Lg==
X-Orig-Path satorlaser.homedns.org!not-for-mail
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130509 Thunderbird/17.0.6
In-Reply-To <56f10268-0e26-4f86-ae03-8fd740cc4544@googlegroups.com>
Xref csiph.com comp.lang.python:47553

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:04 -0700
  Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:16 -0700
    Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:18 -0700
      Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:29 -0700
        Re: Sorting a set works, sorting a dictionary fails ? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-10 10:27 +0100
          Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 02:32 -0700
            Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 02:48 -0700
              Re: Sorting a set works, sorting a dictionary fails ? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-10 11:13 +0100
              Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 15:11 +0200
        Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 11:40 +0200
          Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 03:20 -0700
          Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 03:42 -0700
            Re: Sorting a set works, sorting a dictionary fails ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-10 11:47 +0000
            Re: Sorting a set works, sorting a dictionary fails ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-11 02:14 +0000
        Re: Sorting a set works, sorting a dictionary fails ? Larry Hudson <orgnut@yahoo.com> - 2013-06-11 02:16 -0700
  Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 11:19 +0200

csiph-web