Sorting a set works, sorting a dictionary fails ?

Discussion in 'Python' started by Íéêüëáïò Êïýñáò, Jun 10, 2013.

  1. 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?
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #1
    1. Advertising

  2. 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?
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #2
    1. Advertising

  3. Ôç ÄåõôÝñá, 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()):
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #3
  4. 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.
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #4
  5. 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
     
    Ulrich Eckhardt, Jun 10, 2013
    #5
  6. On 10 Jun 2013 09:34, "Íéêüëáïò Êïýñáò" <> 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())"
     
    Fábio Santos, Jun 10, 2013
    #6
  7. 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
     
    Ulrich Eckhardt, Jun 10, 2013
    #7
  8. 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?
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #8
  9. On 10 Jun 2013 10:53, "Íéêüëáïò Êïýñáò" <> 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.
     
    Fábio Santos, Jun 10, 2013
    #9
  10. Ôç ÄåõôÝñá, 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?
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #10
  11. 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.
     
    Íéêüëáïò Êïýñáò, Jun 10, 2013
    #11
  12. 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
     
    Steven D'Aprano, Jun 10, 2013
    #12
  13. 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
     
    Ulrich Eckhardt, Jun 10, 2013
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    453
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    337
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,158
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    540
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    198
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page