problem with sorting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ankitks.mital@gmail.com

    problem with sorting

    >>dict = {'M':3, 'R':0, 'S':2}
    >>print dict
    {'S': 2, 'R': 0, 'M': 3}

    now if I wanted sorted values in list, i am not able to do this
    >>print dict.values().s ort()
    None

    it returns None instead of [0, 2, 3]

  • Paddy

    #2
    Re: problem with sorting

    On Mar 28, 5:38 am, ankitks.mi...@g mail.com wrote:
    >dict = {'M':3, 'R':0, 'S':2}
    >print dict
    >
    {'S': 2, 'R': 0, 'M': 3}
    >
    now if I wanted sorted values in list, i am not able to do this>>print dict.values().s ort()
    >
    None
    >
    it returns None instead of [0, 2, 3]
    Try:

    from pprint import pprint as pp
    pp(my_dict)


    It works well for other built-in types too.

    P.S it is not good to use a name, dict, that already has a use in
    Python.

    - Paddy.

    Comment

    • jwelby

      #3
      Re: problem with sorting

      On Mar 28, 5:38 am, ankitks.mi...@g mail.com wrote:
      >dict = {'M':3, 'R':0, 'S':2}
      >print dict
      >
      {'S': 2, 'R': 0, 'M': 3}
      >
      now if I wanted sorted values in list, i am not able to do this>>print dict.values().s ort()
      >
      None
      >
      it returns None instead of [0, 2, 3]
      The sort method works by sorting 'in place'. That means it doesn't
      return the sorted value, but just sorts the sequence.
      >>t = {'M':3, 'R':0, 'S':2}
      >>x = t.values()
      >>x.sort()
      >>x
      [0, 2, 3]

      or you can use sorted(), which does return the sorted sequence:
      >>sorted(t.valu es())
      [0, 2, 3]

      Comment

      • raj

        #4
        Re: problem with sorting

        To ankit:

        Well, sort() doesn't return the sorted list. It returns None. Why not
        this straightforward way?
        dvals = dict.values()
        dvals.sort()
        print dvals

        Comment

        • Duncan Booth

          #5
          Re: problem with sorting

          castironpi@gmai l.com wrote:
          On Mar 28, 1:57ÿam, raj <rajeeshrn...@g mail.comwrote:
          >To ankit:
          >>
          >Well, sort() doesn't return the sorted list. It returns None. Why not
          >this straightforward way?
          >dvals = dict.values()
          >dvals.sort()
          >print dvals
          >
          Why not sorted( dict.values() ).
          >
          If you are going to do it that way then it may be preferable to use
          itervalues:

          print sorted(dict.ite rvalues())

          Both this and raj's suggestion create a single sorted list. Your suggestion
          creates two lists: the unsorted one and a separate sorted one. In most
          cases the difference is probably insignificant, but if you have a *lot* of
          values it might make a difference.

          Comment

          • bearophileHUGS@lycos.com

            #6
            Re: problem with sorting

            Duncan Booth:
            Both this and raj's suggestion create a single sorted list. Your suggestion
            creates two lists: the unsorted one and a separate sorted one. In most
            cases the difference is probably insignificant, but if you have a *lot* of
            values it might make a difference.
            The good thing of Python 3.0 is that it forces you to do the right
            thing here :-)

            Bye,
            bearophile

            Comment

            Working...