sort problem

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

    #1

    sort problem

    I have a list of lists (a grid table) that can have about 15000 - 20000
    "rows" and 10 "cols", so:

    1 [ [ 'aaa', 'vv', 'cc', 23, ... ],
    2 [ 'aav', 'vv', 'cc', 45, ... ],
    ....
    15000 [ 'sad', 'ad', 'es', 123, ... ], ]

    I need to sort this list, but I need to specify two things: the "column"
    and its type (string or int), so for example in this list, I want to
    sort the fourth column that has int values. The type because that I want
    that 1, 2, 12 will be sort in this order, not 1, 12, 2 like strings.

    I have already tried to modify some code found on aspn, but the results
    are always too slow for me, about 30-40 sec.
    Can someone has some code or some point where can I start for speedup my
    code?

    Thanks,
    Michele
  • Lasse Vågsæther Karlsen

    #2
    Re: sort problem

    How about:

    list.sort(key=l ambda x: x[3])

    Does that work?

    Comment

    • Michele Petrazzo

      #3
      Re: sort problem

      Lasse Vågsæther Karlsen wrote:[color=blue]
      > How about:
      >
      > list.sort(key=l ambda x: x[3])
      >
      > Does that work?
      >[/color]

      Yes, on my linux-test-box it work, but I my developer pc I don't have
      the 2.4 yet. I think that this is a good reason for update :)

      Thanks,
      Michele

      Comment

      • Kent Johnson

        #4
        Re: sort problem

        Michele Petrazzo wrote:[color=blue]
        > Lasse Vågsæther Karlsen wrote:
        >[color=green]
        >> How about:
        >>
        >> list.sort(key=l ambda x: x[3])[/color][/color]

        Better to use key=operator.it emgetter(3)
        [color=blue]
        > Yes, on my linux-test-box it work, but I my developer pc I don't have
        > the 2.4 yet. I think that this is a good reason for update :)[/color]

        or learn about decorate-sort-undecorate:

        lst = [ ...whatever ]
        lst = [ x[3], i, x for i, x in enumerate(lst) ]
        lst.sort()
        lst = [ x for _, _, x in lst ]

        Kent
        [color=blue]
        >
        > Thanks,
        > Michele[/color]

        Comment

        • Michele Petrazzo

          #5
          Re: sort problem

          Kent Johnson wrote:[color=blue]
          > or learn about decorate-sort-undecorate:
          >
          > lst = [ ...whatever ] lst = [ x[3], i, x for i, x in enumerate(lst) ]
          >[/color]
          I think that here the code must be changed (for the future):
          lst = [ (x[3], i, x) for i, x in enumerate(lst) ]
          [color=blue]
          > lst.sort() lst = [ x for _, _, x in lst ][/color]


          Wow, this work with my py 2.3!
          [color=blue]
          >
          > Kent
          >[/color]

          Thanks,
          Michele

          Comment

          • Alex Martelli

            #6
            Re: sort problem

            Michele Petrazzo <michele.petraz zo@TOGLIunipex. it> wrote:
            [color=blue]
            > Lasse Vågsæther Karlsen wrote:[color=green]
            > > How about:
            > >
            > > list.sort(key=l ambda x: x[3])
            > >
            > > Does that work?[/color]
            >
            > Yes, on my linux-test-box it work, but I my developer pc I don't have
            > the 2.4 yet. I think that this is a good reason for update :)[/color]

            Updating is a good idea, and will let you get even faster by avoiding
            the lambda:

            import operator

            thelist.sort(ke y=operator.item getter(3))

            However, until you can upgrade you might be happy enough with a direct
            implementation of the decorate-sort-undecorate (DSU) idiom which they
            new "key=" named argument to sort implements. To wit:

            aux = [ (x[3], x) for x in thelist ]
            aux.sort()
            thelist[:] = [ x[-1] for x in aux ]

            Note that the "decoration " can include as many "columns" as you want,
            transformations obtained by calling int(...) or str(...) on some of the
            columns, and so on. This applies to "key=" in 2.4 just as well as to
            the (slightly slower) direct implementation in 2.3 and earlier.


            Alex

            Comment

            Working...