pythonic way to sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    #1

    pythonic way to sort

    hi
    I have a file with columns delimited by '~' like this:

    1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
    2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
    3SOME STRING ~ACD~1435345355 4G~20060401~000 00000

    ......

    What is the pythonic way to sort this type of structured text file?
    Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes

    1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
    3SOME STRING ~ACD~1435345355 4G~20060401~000 00000
    2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
    ?
    I know for a start, that i have to split on '~', then append all the
    second columns into a list, then sort the list using sort(), but i am
    stuck with how to get the rest of the corresponding columns after the
    sort....

    thanks...

  • Robert Kern

    #2
    Re: pythonic way to sort

    micklee74@hotma il.com wrote:[color=blue]
    > hi
    > I have a file with columns delimited by '~' like this:
    >
    > 1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
    > 2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
    > 3SOME STRING ~ACD~1435345355 4G~20060401~000 00000
    >
    > .....
    >
    > What is the pythonic way to sort this type of structured text file?
    > Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes
    >
    > 1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
    > 3SOME STRING ~ACD~1435345355 4G~20060401~000 00000
    > 2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
    > ?
    > I know for a start, that i have to split on '~', then append all the
    > second columns into a list, then sort the list using sort(), but i am
    > stuck with how to get the rest of the corresponding columns after the
    > sort....[/color]

    In Python 2.4 and up, you can use the key= keyword to list.sort(). E.g.

    In [2]: text = """1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
    ...: 2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
    ...: 3SOME STRING ~ACD~1435345355 4G~20060401~000 00000"""

    In [3]: lines = text.split('\n' )

    In [4]: lines
    Out[4]:
    ['1SOME STRING ~ABC~1231123243 2D~20060401~000 00000',
    '2SOME STRING ~DEF~1353453454 3C~20060401~000 00000',
    '3SOME STRING ~ACD~1435345355 4G~20060401~000 00000']

    In [5]: lines.sort(key= lambda x: x.split('~')[1])

    In [6]: lines
    Out[6]:
    ['1SOME STRING ~ABC~1231123243 2D~20060401~000 00000',
    '3SOME STRING ~ACD~1435345355 4G~20060401~000 00000',
    '2SOME STRING ~DEF~1353453454 3C~20060401~000 00000']

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • Jay Parlar

      #3
      Re: pythonic way to sort


      On May 4, 2006, at 12:12 AM, micklee74@hotma il.com wrote:
      [color=blue]
      > hi
      > I have a file with columns delimited by '~' like this:
      >
      > 1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
      > 2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
      > 3SOME STRING ~ACD~1435345355 4G~20060401~000 00000
      >
      > .....
      >
      > What is the pythonic way to sort this type of structured text file?
      > Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes
      >
      > 1SOME STRING ~ABC~1231123243 2D~20060401~000 00000
      > 3SOME STRING ~ACD~1435345355 4G~20060401~000 00000
      > 2SOME STRING ~DEF~1353453454 3C~20060401~000 00000
      > ?
      > I know for a start, that i have to split on '~', then append all the
      > second columns into a list, then sort the list using sort(), but i am
      > stuck with how to get the rest of the corresponding columns after the
      > sort....
      >
      > thanks...
      >[/color]

      A couple ways. Assume that you have the lines in a list called 'lines',
      as follows:

      lines = [
      "1SOME STRING ~ABC~1231123243 2D~20060401~000 00000",
      "3SOME STRING ~ACD~1435345355 4G~20060401~000 00000",
      "2SOME STRING ~DEF~1353453454 3C~20060401~000 00000"]


      The more traditional way would be to define your own comparison
      function:

      def my_cmp(x,y):
      return cmp( x.split("~")[1], y.split("~")[1])

      lines.sort(cmp= my_cmp)


      The newer, faster way, would be to define your own key function:

      def my_key(x):
      return x.split("~")[1]

      lines.sort(key= my_key)


      The key function is faster because you only have to do the
      split("~")[1] once for each line, whereas it will be done many times
      for each line if you use a comparison function.

      Jay P.

      Comment

      • Boris Borcic

        #4
        Re: pythonic way to sort

        Jay Parlar wrote:[color=blue]
        >
        > On May 4, 2006, at 12:12 AM, micklee74@hotma il.com wrote:
        > [...][/color]
        [color=blue]
        > Assume that you have the lines in a list called 'lines',
        > as follows:
        >
        > lines = [
        > "1SOME STRING ~ABC~1231123243 2D~20060401~000 00000",
        > "3SOME STRING ~ACD~1435345355 4G~20060401~000 00000",
        > "2SOME STRING ~DEF~1353453454 3C~20060401~000 00000"]
        >
        >
        > The more traditional way would be to define your own comparison function:
        >
        > def my_cmp(x,y):
        > return cmp( x.split("~")[1], y.split("~")[1])
        >
        > lines.sort(cmp= my_cmp)
        >
        >
        > The newer, faster way, would be to define your own key function:
        >
        > def my_key(x):
        > return x.split("~")[1]
        >
        > lines.sort(key= my_key)[/color]

        and if the data is in a file rather than a list, you may write eg

        lines = sorted(file("/path/tofile"),key=mi ke)

        to create it sorted.

        Comment

        Working...