Re: define 'in' operator on lists
From: ..:: sjf ::.. (somebody_at_unknown.org)
Date: 10/11/04
- Next message: Alex Martelli: "Re: Why can't I xor strings?"
- Previous message: Bruno Desthuilliers: "Re: How best to return a class method?"
- In reply to: Bengt Richter: "Re: define 'in' operator on lists"
- Next in thread: Michael Hoffman: "Re: define 'in' operator on lists"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 ;-)"
- Next message: Alex Martelli: "Re: Why can't I xor strings?"
- Previous message: Bruno Desthuilliers: "Re: How best to return a class method?"
- In reply to: Bengt Richter: "Re: define 'in' operator on lists"
- Next in thread: Michael Hoffman: "Re: define 'in' operator on lists"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|