By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

Lines of Strings

P: n/a
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/

Sep 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Reem Mohammed wrote:
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 16 '05 #2

P: n/a
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:
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/

Sep 16 '05 #3

P: n/a
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: 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/


Sep 16 '05 #4

P: n/a
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
Sep 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.