How to sort a multi-dimensional list in python 2.3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erbrose
    New Member
    • Oct 2006
    • 58

    How to sort a multi-dimensional list in python 2.3

    Yes I know.. Python 2.3... unfortunately our development servers are RedHat and that is the default version installed on them and apparently, upgrading can cause system failures (trying to figure out a work around to run multiple versions of python, but not there yet) in the mean time... I need see if any one can help me with sorting a multidimensiona l list by certain elements with in that list. I've read about Schwartzian Transform in Python? but for the life of my don't completely understand it. I have a M-D list that looks something like this
    <COL0 , COL1 , COL2 , COL3 , COL4 , COL5>

    0001 , 4 , 0.34 , 3 , 15 , 25.3
    0001 , 4 , 0.34 , 2 , 10 , 20.2
    0002 , 4 , 0.32 , 6 , 11 , 20.1
    0001 , 4 , 0.34 , 3 , 24 , 21.6
    0001 , 4 , 0.34 , 1 , 15 , 20.3


    I need to sort by COL0, COL3, COL4 & COL5
    i know in 2.6 i can just sort and use a key definition to define what columns i need to sort by...but in 2.3 key was not yet around...

    here is something else i was trying where a = the MD list, i read you could sort one list of value by another list... doesn't work though... well... the sort of c, works just fine but the sort of e does not and that is what i need...

    Code:
    for b in a:
        line = str(a[i][0]) + ',' + str(a[i][3]) + ',' + str(a[i][4]) + ',' + str(a[i][5])
        c.append(line.split(','))
        d.append(a)
        i = i + 1
    
    e = zip (c,d)
    
    e.sort()
    c.sort()
    out = [x[1] for x in e]
    print out
    print c
    Thanks ahead of time to any help and or suggestions!
    Cheers,
    Eric
  • woooee
    New Member
    • Mar 2008
    • 43

    #2
    From the Python Sort Wiki
    Many constructs given in this HOWTO assume Python 2.4 or later. Before that, there was no sorted() builtin and list.sort() took no keyword arguments. Instead, all of the Py2.x versions supported a cmp parameter to handle user specified comparison functions.
    Code:
    def numeric_compare(x, y):
            return x - y
    sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
    [1, 2, 3, 4, 5]

    Comment

    • erbrose
      New Member
      • Oct 2006
      • 58

      #3
      Thanks for the reply...
      still not sure how that would help me, as its still sorting by all the values and not just select values/columns in my MD List.

      Comment

      • woooee
        New Member
        • Mar 2008
        • 43

        #4
        You would use a function to compare the columns, but since you are unwilling or unable to even give that a try, there is an even easier-to-understand way. Create a second list with each element containing two tuples. The first tuple contains only the columns that you want to sort in the order that you want to sort them (4 elements each). The second tuple contains the original record (6 elements each). You sort normally and will have the entire record in the second tuple, sorted in the order to want, i.e. on the first tuple.

        Comment

        • erbrose
          New Member
          • Oct 2006
          • 58

          #5
          sorry,
          didn't mean to imply that i wasn't willing or able to try. I definitely worked on the tuple method yesterday and still did not sort correctly. the code that i originally posted was that method... well l least thats what i thought that method was.. where e was the tuple
          Code:
          a = []
          d = []
          i = 0
          c = []
          
          
          line = "0001 , 4 , 0.34 , 3 , 15 , 25.3"
          a.append(line.split(','))
          line = "0001 , 4 , 0.35 , 2 , 10 , 20.2"
          a.append(line.split(','))
          
          for b in a:
              line = (a[i][0]) + ',' + (a[i][3]) + ',' + (a[i][4]) + ',' + (a[i][5])
              c.append(line.split(','))
              d.append(a)
              i = i + 1
          
          e = zip(c,d)
          #print e
          e.sort()
          #c.sort()
          out = [x[1] for x in e]
          print out
          alright, so this seems like it almost works.. ie e is sorted based on my values.. but now I guess i am stuck on trying to get the original values back out of e... the line
          out = [x[1] for x in e]
          does not seem to be doing it.
          Thank again for your help! I do appreciate it and i didn't mean to sound defeated!

          Comment

          • erbrose
            New Member
            • Oct 2006
            • 58

            #6
            oh... i found my error...
            in line 15 i was appending all of a to d, then joining c and d into tuple e. what i should have done is just join c and a into e...
            my out is now correct... thank you so much!

            Comment

            • woooee
              New Member
              • Mar 2008
              • 43

              #7
              Since you are sorting numbers, you want to convert to a float as well. Strings sort left to right, so "10" will sort before "2", because "1" is less than "2". Note that floats have limited precision so can be slightly different than the original number but work fine for "normal" calculations.
              Code:
              a = []
              line = "0001 , 4 , 0.34 , 3 , 15 , 25.3"
              a.append(line.split(','))
              line = "0001 , 4 , 0.35 , 2 , 10 , 20.2"
              a.append(line.split(','))
              c = [] 
              
              for b in a:
                  c.append( ((float(b[0]), float(b[3]), float(b[4]), float(b[5])), (b)) )
              c.sort()
              for b in c:
                  print b[1]

              Comment

              • erbrose
                New Member
                • Oct 2006
                • 58

                #8
                thanks... that one took me a while to figure out too! :)

                Comment

                Working...