Lines of Strings

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

    #1

    Lines of Strings

    Hi

    Suppose we have data file like this one (Consider all lines as strings )

    1 2 3 3 4 4 4 4 5 6
    2 2 2 5 5 5 6
    3 2 1 1 1 3 3 3 4 6

    I would like to remove line if its belong to another one, and will be able
    to do this if longer line come after a short one.

    Thanks

    _______________ _______________ _______________ _______________ _____
    Express yourself instantly with MSN Messenger! Download today - it's FREE!


  • bruno modulix

    #2
    Re: Lines of Strings

    Reem Mohammed wrote:[color=blue]
    > Hi
    >
    > Suppose we have data file like this one (Consider all lines as strings )
    >
    > 1 2 3 3 4 4 4 4 5 6
    > 2 2 2 5 5 5 6
    > 3 2 1 1 1 3 3 3 4 6
    >
    > I would like to remove line if its belong to another one, and will be
    > able to do this if longer line come after a short one.[/color]

    That's fine. Now try and do it, and ask for help when you're in trouble.

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Larry Bates

      #3
      Re: Lines of Strings

      If these were the lines what would the output look like?
      From your example it doesn't appear that any of the lines
      would be eliminated.

      Larry Bates

      Reem Mohammed wrote:[color=blue]
      > Hi
      >
      > Suppose we have data file like this one (Consider all lines as strings )
      >
      > 1 2 3 3 4 4 4 4 5 6
      > 2 2 2 5 5 5 6
      > 3 2 1 1 1 3 3 3 4 6
      >
      > I would like to remove line if its belong to another one, and will be
      > able to do this if longer line come after a short one.
      >
      > Thanks
      >
      > _______________ _______________ _______________ _______________ _____
      > Express yourself instantly with MSN Messenger! Download today - it's
      > FREE! http://messenger.msn.click-url.com/g...ave/direct/01/
      >[/color]

      Comment

      • Larry Bates

        #4
        Re: Lines of Strings

        If these were the lines what would the output look like?[color=blue]
        >From your example it doesn't appear that any of the lines[/color]
        would be eliminated.

        Larry Bates

        Reem Mohammed wrote:[color=blue]
        > Hi
        >
        > Suppose we have data file like this one (Consider all lines as strings )
        >
        > 1 2 3 3 4 4 4 4 5 6
        > 2 2 2 5 5 5 6
        > 3 2 1 1 1 3 3 3 4 6
        >
        > I would like to remove line if its belong to another one, and will be
        > able to do this if longer line come after a short one.
        >
        > Thanks
        >
        > _______________ _______________ _______________ _______________ _____
        > Express yourself instantly with MSN Messenger! Download today - it's
        > FREE! http://messenger.msn.click-url.com/g...ave/direct/01/
        >[/color]

        Comment

        • Tom Anderson

          #5
          Re: Lines of Strings

          On Thu, 15 Sep 2005, Reem Mohammed wrote:
          [color=blue]
          > Suppose we have data file like this one (Consider all lines as strings )
          >
          > 1 2 3 3 4 4 4 4 5 6
          > 2 2 2 5 5 5 6
          > 3 2 1 1 1 3 3 3 4 6
          >
          > I would like to remove line if its belong to another one, and will be
          > able to do this if longer line come after a short one.[/color]

          when you say "belong to another one", do you mean "is a substring of
          another one"? so 4 5 6 would belong to 1 2 3 4 5 6 7 8?

          if so, what you're asking for is the set of upper bounds of a partially
          ordered set. i often find that i need to compute things like this; i
          haven't figured out a way to do it any faster than the obvious:

          def upperbounds(set , order):
          """Finds the upper bounds of a set under a partial order.

          Set is an iterable (which may contain duplicates - it doesn't actually
          need to be a set), and order is a function of two arguments such that
          order(a, b) returns True if a is greater than b, and False otherwise.

          """
          bounds = [] # this would be better as a set, really
          for item in set:
          for bound in bounds:
          if (order(bound, item)):
          break
          if (order(item, bound)):
          bounds.remove(b ound)
          else:
          bounds.append(i tem)
          return bounds

          you could use this as follows:

          lines = map(str.strip, inputfile.readl ines())
          print upperbounds(lin es, str.__contains_ _)

          tom

          --
          I content myself with the Speculative part [...], I care not for the Practick. I seldom bring any thing to use, 'tis not my way. Knowledge is my ultimate end. -- Sir Nicholas Gimcrack

          Comment

          Working...