Question on sorting

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

    #1

    Question on sorting

    Hi,
    I have a file of records of 4 fields each.
    Each field is separated by a semicolon. That is

    Filed1;Ffield2; Field3;Field4

    But there may be also empty records such as
    ;;;;
    (only semicolons).

    For sorting I used
    ############### ##
    lines = file('Config.tx t').readlines() # a file I want to sort
    lines.sort()
    ff=open('Config Sorted.txt','w' )# sorted file
    ff.writelines(l ines)
    ff.close()
    ###############
    It was sorted but empty records were first. I need them to be last(at
    the end of the file). How can I do that?

    Thanks for help
    Lad
  • wes weston

    #2
    Re: Question on sorting

    Lad wrote:[color=blue]
    > Hi,
    > I have a file of records of 4 fields each.
    > Each field is separated by a semicolon. That is
    >
    > Filed1;Ffield2; Field3;Field4
    >
    > But there may be also empty records such as
    > ;;;;
    > (only semicolons).
    >
    > For sorting I used
    > ############### ##
    > lines = file('Config.tx t').readlines() # a file I want to sort
    > lines.sort()
    > ff=open('Config Sorted.txt','w' )# sorted file
    > ff.writelines(l ines)
    > ff.close()
    > ###############
    > It was sorted but empty records were first. I need them to be last(at
    > the end of the file). How can I do that?
    >
    > Thanks for help
    > Lad[/color]

    Lad,
    The sort call can have a function name as an arg. You
    could do:

    def mycompare(s1,s2 ):
    #return -1 to put s1's at front; 1 to put s1's at back; 0 for a tie
    #if s1==";;;;" and s2<>";;;;": return 1

    lines.sort(myco mpare)

    wes

    Comment

    • Eddie Corns

      #3
      Re: Question on sorting

      wes weston <wweston@att.ne t> writes:
      [color=blue]
      >Lad wrote:[color=green]
      >> Hi,
      >> I have a file of records of 4 fields each.
      >> Each field is separated by a semicolon. That is
      >>
      >> Filed1;Ffield2; Field3;Field4
      >>
      >> But there may be also empty records such as
      >> ;;;;
      >> (only semicolons).
      >>
      >> For sorting I used
      >> ############### ##
      >> lines = file('Config.tx t').readlines() # a file I want to sort
      >> lines.sort()
      >> ff=open('Config Sorted.txt','w' )# sorted file
      >> ff.writelines(l ines)
      >> ff.close()
      >> ###############
      >> It was sorted but empty records were first. I need them to be last(at
      >> the end of the file). How can I do that?
      >>
      >> Thanks for help
      >> Lad[/color][/color]
      [color=blue]
      >Lad,
      > The sort call can have a function name as an arg. You
      >could do:[/color]
      [color=blue]
      >def mycompare(s1,s2 ):
      >#return -1 to put s1's at front; 1 to put s1's at back; 0 for a tie
      >#if s1==";;;;" and s2<>";;;;": return 1[/color]
      [color=blue]
      >lines.sort(myc ompare)[/color]

      I can't help feeling that the OP might have really wanted to be sorting on
      individual fields rather than whole lines. In which case I would think of
      doing a line.split(';') on each line before sorting. It would still need
      either to use a function to make empty fields go later or alternatively use
      DSU (google!) and convert '' to say '~' and back again. This also solves the
      problem of what to expect when only some of the fields are blank rather than
      all of them.

      Eddie

      Comment

      • Lad

        #4
        Re: Question on sorting

        wes weston <wweston@att.ne t> wrote in message news:<0gKqd.722 52$7i4.43429@bg tnsc05-news.ops.worldn et.att.net>...[color=blue]
        > Lad wrote:[color=green]
        > > Hi,
        > > I have a file of records of 4 fields each.
        > > Each field is separated by a semicolon. That is
        > >
        > > Filed1;Ffield2; Field3;Field4
        > >
        > > But there may be also empty records such as
        > > ;;;;
        > > (only semicolons).
        > >
        > > For sorting I used
        > > ############### ##
        > > lines = file('Config.tx t').readlines() # a file I want to sort
        > > lines.sort()
        > > ff=open('Config Sorted.txt','w' )# sorted file
        > > ff.writelines(l ines)
        > > ff.close()
        > > ###############
        > > It was sorted but empty records were first. I need them to be last(at
        > > the end of the file). How can I do that?
        > >
        > > Thanks for help
        > > Lad[/color]
        >
        > Lad,
        > The sort call can have a function name as an arg. You
        > could do:
        >
        > def mycompare(s1,s2 ):
        > #return -1 to put s1's at front; 1 to put s1's at back; 0 for a tie
        > #if s1==";;;;" and s2<>";;;;": return 1
        >
        > lines.sort(myco mpare)
        >[/color]
        Wes,
        Thank you for reply. But I do not understand mycompare function. Can
        you please explain to me how it should work? Thanks

        LAd

        Comment

        • Peter Otten

          #5
          Re: Question on sorting

          Lad wrote:
          [color=blue]
          > wes weston <wweston@att.ne t> wrote in message
          > news:<0gKqd.722 52$7i4.43429@bg tnsc05-news.ops.worldn et.att.net>...[color=green]
          >> Lad wrote:[color=darkred]
          >> > Hi,
          >> > I have a file of records of 4 fields each.
          >> > Each field is separated by a semicolon. That is
          >> >
          >> > Filed1;Ffield2; Field3;Field4
          >> >
          >> > But there may be also empty records such as
          >> > ;;;;
          >> > (only semicolons).
          >> >
          >> > For sorting I used
          >> > ############### ##
          >> > lines = file('Config.tx t').readlines() # a file I want to sort
          >> > lines.sort()
          >> > ff=open('Config Sorted.txt','w' )# sorted file
          >> > ff.writelines(l ines)
          >> > ff.close()
          >> > ###############
          >> > It was sorted but empty records were first. I need them to be last(at
          >> > the end of the file). How can I do that?
          >> >
          >> > Thanks for help
          >> > Lad[/color]
          >>
          >> Lad,
          >> The sort call can have a function name as an arg. You
          >> could do:
          >>
          >> def mycompare(s1,s2 ):
          >> #return -1 to put s1's at front; 1 to put s1's at back; 0 for a tie
          >> #if s1==";;;;" and s2<>";;;;": return 1
          >>
          >> lines.sort(myco mpare)
          >>[/color]
          > Wes,
          > Thank you for reply. But I do not understand mycompare function. Can
          > you please explain to me how it should work? Thanks[/color]

          compare(a, b) is just a function for comparing two items/lines. I must
          return -1 if a<b, +1 if a>b, and 0 if a==b. For example the following
          compare moves the ";;;;" records to the end and keeps the order of others
          unaffected:
          [color=blue][color=green][color=darkred]
          >>> items = [";;;;", ";a;b;;", ";b;a;;", "a;b;c;d;e" , "a;;;d;e"]
          >>> def compare(a, b):[/color][/color][/color]
          .... return cmp(a == ";;;;", b == ";;;;") or cmp(a, b)
          ....[color=blue][color=green][color=darkred]
          >>> items.sort(comp are)
          >>> items[/color][/color][/color]
          [';a;b;;', ';b;a;;', 'a;;;d;e', 'a;b;c;d;e', ';;;;']

          As Eddie Corns pointed out, you left some doubt whether that is really what
          you want. Here is a more complex compare() that handles the lines as
          columns split by ";" and puts empty columns last in the sorting order:
          [color=blue][color=green][color=darkred]
          >>> def key(row):[/color][/color][/color]
          .... return [(not col, col) for col in row.split(";")]
          ....[color=blue][color=green][color=darkred]
          >>> def compare(a, b):[/color][/color][/color]
          .... return cmp(key(a), key(b))
          ....[color=blue][color=green][color=darkred]
          >>> items.sort(comp are)
          >>> items[/color][/color][/color]
          ['a;b;c;d;e', 'a;;;d;e', ';a;b;;', ';b;a;;', ';;;;']

          If you are on Python 2.4, you don't need the compare() detour and can use
          key() directly:
          [color=blue][color=green][color=darkred]
          >>> items.sort(key= key)
          >>> items[/color][/color][/color]
          ['a;b;c;d;e', 'a;;;d;e', ';a;b;;', ';b;a;;', ';;;;']

          Finally, the ";;;;" lines don't seem to carry any information - why not
          filter them out completely?
          [color=blue][color=green][color=darkred]
          >>> items = [line[:-1] for line in file("cfg.txt", "U") if line != ";;;;\n"][/color][/color][/color]


          Peter

          Comment

          • Lad

            #6
            Re: Question on sorting

            Peter Otten <__peter__@web. de> wrote in message news:<cokacu$fp 6$04$1@news.t-online.com>...[color=blue]
            > Lad wrote:
            >[color=green]
            > > wes weston <wweston@att.ne t> wrote in message
            > > news:<0gKqd.722 52$7i4.43429@bg tnsc05-news.ops.worldn et.att.net>...[color=darkred]
            > >> Lad wrote:
            > >> > Hi,
            > >> > I have a file of records of 4 fields each.
            > >> > Each field is separated by a semicolon. That is
            > >> >
            > >> > Filed1;Ffield2; Field3;Field4
            > >> >
            > >> > But there may be also empty records such as
            > >> > ;;;;
            > >> > (only semicolons).
            > >> >
            > >> > For sorting I used
            > >> > ############### ##
            > >> > lines = file('Config.tx t').readlines() # a file I want to sort
            > >> > lines.sort()
            > >> > ff=open('Config Sorted.txt','w' )# sorted file
            > >> > ff.writelines(l ines)
            > >> > ff.close()
            > >> > ###############
            > >> > It was sorted but empty records were first. I need them to be last(at
            > >> > the end of the file). How can I do that?
            > >> >
            > >> > Thanks for help
            > >> > Lad
            > >>
            > >> Lad,
            > >> The sort call can have a function name as an arg. You
            > >> could do:
            > >>
            > >> def mycompare(s1,s2 ):
            > >> #return -1 to put s1's at front; 1 to put s1's at back; 0 for a tie
            > >> #if s1==";;;;" and s2<>";;;;": return 1
            > >>
            > >> lines.sort(myco mpare)
            > >>[/color]
            > > Wes,
            > > Thank you for reply. But I do not understand mycompare function. Can
            > > you please explain to me how it should work? Thanks[/color]
            >
            > compare(a, b) is just a function for comparing two items/lines. I must
            > return -1 if a<b, +1 if a>b, and 0 if a==b. For example the following
            > compare moves the ";;;;" records to the end and keeps the order of others
            > unaffected:
            >[color=green][color=darkred]
            > >>> items = [";;;;", ";a;b;;", ";b;a;;", "a;b;c;d;e" , "a;;;d;e"]
            > >>> def compare(a, b):[/color][/color]
            > ... return cmp(a == ";;;;", b == ";;;;") or cmp(a, b)
            > ...[color=green][color=darkred]
            > >>> items.sort(comp are)
            > >>> items[/color][/color]
            > [';a;b;;', ';b;a;;', 'a;;;d;e', 'a;b;c;d;e', ';;;;']
            >[/color]
            Petr,
            thank you for help and explanation.
            It works
            Lad

            Comment

            Working...