sorting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashishbathini
    New Member
    • Nov 2007
    • 9

    sorting

    I am very new to python ... I am using windows xp

    i am writing a program in python , in which i want to sort the first elements of say 5 arrays

    eg :
    Code:
    a = [3,2,1]
    b=[7,5,3]
    c=[56,242,4]
    r=[3,146,124]
    so i used zip to make them in to a single array
    Code:
    t= zip(a,b,c,d)
    which = >
    t= (a[0],b[0],c[0],r[0]),a[1],b[1],............r( 2))

    now i used y=t(:1)
    => y = (a[0],b[0],c[0])


    now if i say
    y.sort()
    print y


    the result is y but its not gettin sorted
    Last edited by bartonc; Nov 20 '07, 03:19 AM. Reason: Added [CODE][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by ashishbathini
    I am very new to python ... I am using windows xp

    i am writing a program in python , in which i want to sort the first elements of say 5 arrays

    eg :
    Code:
    a = [3,2,1]
    b=[7,5,3]
    c=[56,242,4]
    r=[3,146,124]
    so i used zip to make them in to a single array
    Code:
    t= zip(a,b,c,d)
    which = >
    t= (a[0],b[0],c[0],r[0]),a[1],b[1],............r( 2))

    now i used y=t(:1)
    => y = (a[0],b[0],c[0])


    now if i say
    y.sort()
    print y


    the result is y but its not gettin sorted
    I didn't try zip(). I simply add the lists together:[CODE=python]a = [3, 2, 1]
    b = [7, 5, 3]
    c = [56, 242, 4]
    r = [3, 146, 124]

    t = a + b + c + r
    print t
    # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124]
    t.sort()
    print t
    # [1, 2, 3, 3, 3, 4, 5, 7, 56, 124, 146, 242][/CODE]

    Comment

    • ashishbathini
      New Member
      • Nov 2007
      • 9

      #3
      Originally posted by bartonc
      I didn't try zip(). I simply add the lists together:[CODE=python]a = [3, 2, 1]
      b = [7, 5, 3]
      c = [56, 242, 4]
      r = [3, 146, 124]

      t = a + b + c + r
      print t
      # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124]
      t.sort()
      print t
      # [1, 2, 3, 3, 3, 4, 5, 7, 56, 124, 146, 242][/CODE]



      here what i'm trying to do is ......
      i want to sort the elements

      a[o],b[0].c[0]

      thats all ... i dont want to sort all the arrays ...

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        I didn't try zip(). I simply add the lists together:[CODE=python]a = [3, 2, 1]
        b = [7, 5, 3]
        c = [56, 242, 4]
        r = [3, 146, 124]

        t = a + b + c + r
        print t
        # [3, 2, 1, 7, 5, 3, 56, 242, 4, 3, 146, 124]
        t.sort()
        print t
        # [1, 2, 3, 3, 3, 4, 5, 7, 56, 124, 146, 242][/CODE]
        If zip()ing all the nth items together is what you are really after, then this will work:[CODE=python]
        a = [3, 2, 1]
        b = [7, 5, 3]
        c = [56, 242, 4]
        r = [3, 146, 124]

        t = zip(a, b, c, r)
        print t
        # [(3, 7, 56, 3), (2, 5, 242, 146), (1, 3, 4, 124)]
        for i, intTuple in enumerate(t):
        t[i] = sorted(intTuple )
        print t
        # [[3, 3, 7, 56], [2, 5, 146, 242], [1, 3, 4, 124]]
        t.sort()
        print t
        # [[1, 3, 4, 124], [2, 5, 146, 242], [3, 3, 7, 56]][/CODE]

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by bartonc
          If zip()ing all the nth items together is what you are really after, then this will work:[CODE=python]
          a = [3, 2, 1]
          b = [7, 5, 3]
          c = [56, 242, 4]
          r = [3, 146, 124]

          t = zip(a, b, c, r)
          print t
          # [(3, 7, 56, 3), (2, 5, 242, 146), (1, 3, 4, 124)]
          for i, intTuple in enumerate(t):
          t[i] = sorted(intTuple )
          print t
          # [[3, 3, 7, 56], [2, 5, 146, 242], [1, 3, 4, 124]]
          t.sort()
          print t
          # [[1, 3, 4, 124], [2, 5, 146, 242], [3, 3, 7, 56]][/CODE]
          If you want to preserve the original lists, order them and sort them individually, you might do this:[CODE=python]
          a = [3, 2, 1]
          b = [7, 5, 3]
          c = [56, 242, 4]
          r = [3, 146, 124]

          t = [a, b, c, r]
          print t
          # [[3, 2, 1], [7, 5, 3], [56, 242, 4], [3, 146, 124]]
          # notice that t is now a list of lists, to tuples #
          for intList in t:
          intList.sort()
          print t
          # [[1, 2, 3], [3, 5, 7], [4, 56, 242], [3, 124, 146]]
          t.sort()
          print t
          # [[1, 2, 3], [3, 5, 7], [3, 124, 146], [4, 56, 242]][/CODE]

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by ashishbathini
            here what i'm trying to do is ......
            i want to sort the elements

            a[o],b[0].c[0]

            thats all ... i dont want to sort all the arrays ...
            If I understand correctly, in order to sort the lists ("arrays") into their proper oder, you'll need to sort the individual lists as well.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by ashishbathini
              I am very new to python ... I am using windows xp

              i am writing a program in python , in which i want to sort the first elements of say 5 arrays

              eg :
              Code:
              a = [3,2,1]
              b=[7,5,3]
              c=[56,242,4]
              r=[3,146,124]
              so i used zip to make them in to a single array
              Code:
              t= zip(a,b,c,d)
              which = >
              t= (a[0],b[0],c[0],r[0]),a[1],b[1],............r( 2))

              now i used y=t(:1)
              => y = (a[0],b[0],c[0])


              now if i say
              y.sort()
              print y


              the result is y but its not gettin sorted
              It looks like you are trying to do this:[code=Python]a = [3,2,1]
              b = [7,5,3]
              c = [56,242,4]
              r = [3,146,124]

              sort_list1 = [r,b,c,a]
              sort_list1.sort ()
              print sort_list1


              sort_list2 = [a,b,c,r]
              sort_list2.sort ()
              print sort_list2[/code][code=Python]>>> [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
              [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
              >>> [/code]

              Comment

              • ashishbathini
                New Member
                • Nov 2007
                • 9

                #8
                Originally posted by bvdet
                It looks like you are trying to do this:[code=Python]a = [3,2,1]
                b = [7,5,3]
                c = [56,242,4]
                r = [3,146,124]

                sort_list1 = [r,b,c,a]
                sort_list1.sort ()
                print sort_list1


                sort_list2 = [a,b,c,r]
                sort_list2.sort ()
                print sort_list2[/code][code=Python]>>> [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
                [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]
                >>> [/code]



                in the above example if i have to print just the numbers of the first tuple
                i.e

                say in [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]

                how do i print only [3,2,1 ]
                or [3,146,124]

                bcos i tried

                for i in range(1):
                print

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by ashishbathini
                  in the above example if i have to print just the numbers of the first tuple
                  i.e

                  say in [[3, 2, 1], [3, 146, 124], [7, 5, 3], [56, 242, 4]]

                  how do i print only [3,2,1 ]
                  or [3,146,124]

                  bcos i tried

                  for i in range(1):
                  print
                  Just like this:
                  [CODE=python]print sort_list2[0]
                  # or
                  print sort_list2[1]
                  # or
                  for itme in sort_list2:
                  print item[/CODE]
                  Last edited by bartonc; Nov 20 '07, 06:10 AM.

                  Comment

                  Working...