Dictionary sorting problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JerryB

    #1

    Dictionary sorting problem

    Hi,
    I have a dictionary for counting ocurrences of strings in a document.
    The dictionary looks like this:

    'hello':135
    'goodbye':30
    'lucy':4
    'sky':55
    'diamonds':2398 43
    'yesterday':4

    I want to print the dictionary so I see most common words first:

    'diamonds':2398 43
    'hello':135
    'sky':55
    'goodbye':30
    'lucy':4
    'yesterday':4

    How do I do this? Notice I can't 'swap' the dictionary (making keys
    values and values keys) and sort because I have values like lucy &
    yesterday which have the same number of occurrences.

    Thanks.

  • Irmen de Jong

    #2
    Re: Dictionary sorting problem

    JerryB wrote:[color=blue]
    > Hi,
    > I have a dictionary for counting ocurrences of strings in a document.
    > The dictionary looks like this:
    >
    > 'hello':135
    > 'goodbye':30
    > 'lucy':4
    > 'sky':55
    > 'diamonds':2398 43
    > 'yesterday':4
    >
    > I want to print the dictionary so I see most common words first:
    >
    > 'diamonds':2398 43
    > 'hello':135
    > 'sky':55
    > 'goodbye':30
    > 'lucy':4
    > 'yesterday':4
    >
    > How do I do this? Notice I can't 'swap' the dictionary (making keys
    > values and values keys) and sort because I have values like lucy &
    > yesterday which have the same number of occurrences.
    >
    > Thanks.
    >[/color]

    Don't try to 'swap' the dict, just sort a list based on the items in
    the dict. Try this:

    original= {
    'hello':135,
    'goodbye':30,
    'lucy':4,
    'sky':55,
    'diamonds':2398 43,
    'yesterday':4 }

    items = sorted( (v,k) for (k,v) in original.iterit ems() )
    items.reverse() # depending on what order you want
    print items


    The result is:
    [(239843, 'diamonds'), (135, 'hello'), (55, 'sky'), (30, 'goodbye'), (4, 'yesterday'),
    (4, 'lucy')]


    --Irmen

    Comment

    • Jason  Mobarak

      #3
      Re: Dictionary sorting problem

      You can't sort dictionaries (as implemented by hash tables), they are
      unordered data types, so by definition there's no way to force an order
      on them.



      Comment

      • Bengt Richter

        #4
        Re: Dictionary sorting problem

        On Fri, 16 Sep 2005 21:42:40 +0200, Irmen de Jong <irmen.NOSPAM@x s4all.nl> wrote:
        [color=blue]
        >JerryB wrote:[color=green]
        >> Hi,
        >> I have a dictionary for counting ocurrences of strings in a document.
        >> The dictionary looks like this:
        >>
        >> 'hello':135
        >> 'goodbye':30
        >> 'lucy':4
        >> 'sky':55
        >> 'diamonds':2398 43
        >> 'yesterday':4
        >>
        >> I want to print the dictionary so I see most common words first:
        >>
        >> 'diamonds':2398 43
        >> 'hello':135
        >> 'sky':55
        >> 'goodbye':30
        >> 'lucy':4
        >> 'yesterday':4
        >>
        >> How do I do this? Notice I can't 'swap' the dictionary (making keys
        >> values and values keys) and sort because I have values like lucy &
        >> yesterday which have the same number of occurrences.
        >>
        >> Thanks.
        >>[/color]
        >
        >Don't try to 'swap' the dict, just sort a list based on the items in
        >the dict. Try this:
        >
        >original= {
        >'hello':135,
        >'goodbye':30 ,
        >'lucy':4,
        >'sky':55,
        >'diamonds':239 843,
        >'yesterday': 4 }
        >
        >items = sorted( (v,k) for (k,v) in original.iterit ems() )
        >items.reverse( ) # depending on what order you want
        >print items
        >
        >
        >The result is:
        >[(239843, 'diamonds'), (135, 'hello'), (55, 'sky'), (30, 'goodbye'), (4, 'yesterday'),
        >(4, 'lucy')]
        >
        >[/color]
        or tell sorted what to do ;-)
        [color=blue][color=green][color=darkred]
        >>> original= {[/color][/color][/color]
        ... 'hello':135,
        ... 'goodbye':30,
        ... 'lucy':4,
        ... 'sky':55,
        ... 'diamonds':2398 43,
        ... 'yesterday':4 }[color=blue][color=green][color=darkred]
        >>> list(sorted(ori ginal.iteritems (), None, lambda t:t[1], True))[/color][/color][/color]
        [('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30), ('yesterday', 4), ('lucy',4)]

        Regards,
        Bengt Richter

        Comment

        • Duncan Booth

          #5
          Re: Dictionary sorting problem

          Bengt Richter wrote:
          [color=blue]
          > or tell sorted what to do ;-)
          >[color=green][color=darkred]
          > >>> original= {[/color][/color]
          > ... 'hello':135,
          > ... 'goodbye':30,
          > ... 'lucy':4,
          > ... 'sky':55,
          > ... 'diamonds':2398 43,
          > ... 'yesterday':4 }[color=green][color=darkred]
          > >>> list(sorted(ori ginal.iteritems (), None, lambda t:t[1], True))[/color][/color]
          > [('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30),
          > ('yesterday', 4), ('lucy',4)][/color]

          or a slight variation on this theme which just gives you the keys in value
          order rather than the tuples:
          [color=blue][color=green][color=darkred]
          >>> for k in sorted(original , key=original.ge t, reverse=True):[/color][/color][/color]
          print k, original[k]


          diamonds 239843
          hello 135
          sky 55
          goodbye 30
          yesterday 4
          lucy 4

          Comment

          • Bengt Richter

            #6
            Re: Dictionary sorting problem

            On 17 Sep 2005 11:01:41 GMT, Duncan Booth <duncan.booth@i nvalid.invalid> wrote:
            [color=blue]
            >Bengt Richter wrote:
            >[color=green]
            >> or tell sorted what to do ;-)
            >>[color=darkred]
            >> >>> original= {[/color]
            >> ... 'hello':135,
            >> ... 'goodbye':30,
            >> ... 'lucy':4,
            >> ... 'sky':55,
            >> ... 'diamonds':2398 43,
            >> ... 'yesterday':4 }[color=darkred]
            >> >>> list(sorted(ori ginal.iteritems (), None, lambda t:t[1], True))[/color]
            >> [('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30),
            >> ('yesterday', 4), ('lucy',4)][/color]
            >
            >or a slight variation on this theme which just gives you the keys in value
            >order rather than the tuples:
            >[color=green][color=darkred]
            >>>> for k in sorted(original , key=original.ge t, reverse=True):[/color][/color]
            > print k, original[k]
            >[/color]
            Nice. I like the keyword usage too. Much clearer than my hastypaste ;-)
            [color=blue]
            >
            >diamonds 239843
            >hello 135
            >sky 55
            >goodbye 30
            >yesterday 4
            >lucy 4[/color]

            Regards,
            Bengt Richter

            Comment

            Working...