Re: define 'in' operator on lists

From: ..:: sjf ::.. (somebody_at_unknown.org)
Date: 10/11/04


Date: Mon, 11 Oct 2004 14:01:19 +0200

pewnego dnia niejaki(a) Bengt Richter wstukal(a) byl(a) co nastepuje...:
> On Mon, 11 Oct 2004 12:51:57 +0200, "..:: sjf ::.." <somebody@unknown.org> wrote:
>
>
>>pewnego dnia niejaki(a) Duncan Booth wstukal(a) byl(a) co nastepuje...:
>>
>>>..:: sjf ::.. wrote:
>>>
>>>
>>>
>>>>I want to define 'in' operator (or any "magic" function) on list which
>>>>returns True value if all of list (i.e. _list) elements appears in other
>>>>list (i.e. L) in the same order and False otherwise.
>>>
>>>
>>>How about:
>>>
>>>
>>>
>>>>>>def containedinsequence(short, long):
>>>
>>> ilong = iter(long)
>>> for s in short:
>>> for l in ilong:
>>> if s==l:
>>> break
>>> else:
>>> return False # ran out of long list
>>> return True
>>>
>>>
>>>
>>>>>>L = [1, 2, 3, 4, 5, 6]
>>>>>>containedinsequence([3, 4, 6], L)
>>>
>>OK, this is nearly what I am expecting, but I want if
>>containedsequence([3, 4, 5], L) returns True, but
>>containedsequence([3, 4, 6], L) returns False
>>because that sequence not exist in longlist exactly
>>
>
> Not very tested (just what you see ;-)
>
It seems it works! Thanks very much!

-- 
.:: sjf ::..
"Linux is like Wigwam. No gates, no windows... Apache inside ;-)"


Relevant Pages

  • Re: define in operator on lists
    ... >OK, this is nearly what I am expecting, but I want if ... >because that sequence not exist in longlist exactly ... Regards, ... Bengt Richter ...
    (comp.lang.python)
  • Re: cx_Oracle question
    ... TypeError: expecting a dictionary, sequence or keyword args ...
    (comp.lang.python)
  • Re: all possible combinations
    ... Bengt Richter wrote: ... obscure and generalize it to any size of sequence ('abcdef' or whatever) ...
    (comp.lang.python)
  • How to interpret ...
    ... the following sequence of events? ... I was expecting a blank on line 7, like that on line 2, since this is ... in a sub-process. ... And what does ``export -n'' on earth? ...
    (comp.unix.shell)