Lines of Strings

R

Reem Mohammed

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/go/onm00200471ave/direct/01/
 
B

bruno modulix

Reem said:
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.

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

Larry Bates

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
 
T

Tom Anderson

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.

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(bound)
else:
bounds.append(item)
return bounds

you could use this as follows:

lines = map(str.strip, inputfile.readlines())
print upperbounds(lines, str.__contains__)

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top