Brian van den Broek <[EMAIL PROTECTED]> wrote:

> Ben Bush said unto the world upon 2005-11-14 05:51:
> > I have four lists:
> > lisA=[1,2,3,4,5,6,9]
> > lisB=[1,6,5]
> > lisC=[5,6,3]
> > lisD=[11,14,12,15]
> > how can I write a function to compare lisB, lisC and lisD with lisA, if they
> > share two continuous elements (the order does not matter), then return 1,
> > otherwise return 0. For example, lisA, lisB and lisC have 5,6 or 6,5 so
> > these comparison will return 1 but the comparison between lisD and lisA
> > return 0.

Hijacking Brian's response since my newsserver never god Ben's original
request...:

I would program this at a reasonably high abstraction level, based on
sets -- since you say order doesn't matter, sets are more appropriate
than lists anyway.  For example:

def two_cont_in_common(x, y):
    common = set(x).intersection(y)
    return bool(common.intersection(z+1 for z in common))

i.e., given two lists or whatever, first build the set of all elements
they have in common, then check if that set contains two continuous
elements.  Then, as Brian suggests, you can use this dyadic function on
a reference list and as many others as you wish:

def bens_task(reference, others):
    return [two_cont_in_common(alist, reference) for alist in others]

The call bens_task(lisA, lisB, lisC, lisD) will give you essentially
what you require, except it uses True and False rather than 1 and 0; if
you need to fix this last issue, you can add an int(...) call around
these booleans in either of the functions in question.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to