Lines of Strings

Tom Anderson twic at urchin.earth.li
Sat Sep 17 08:06:48 EDT 2005


On Thu, 15 Sep 2005, Reem Mohammed wrote:

> 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

-- 
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



More information about the Python-list mailing list