compare list

Ben Bush pythonnew at gmail.com
Tue Nov 15 02:35:21 EST 2005


On 11/14/05, Ben Bush <pythonnew at gmail.com> wrote:
>
>
> >
> > 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
>
>  I got invalid syntax error when i run Alex's code. do not know why.
>
>  Alex,
I use PythonWin to run:
def two_cont_in_common(x, y):
common = set(x).intersection(y)
return bool(common.intersection(z+1 for z in common))
def bens_task(reference, others):
return [two_cont_in_common(alist, reference) for alist in others]
 I got invalid syntax error and the mouse's cursor stay on the 'for' in the
statement of
return bool(common.intersection(z+1 for z in common))

--
Thanks!
Ben Bush
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051114/d72f1874/attachment.html>


More information about the Python-list mailing list