Re: define 'in' operator on lists
From: ..:: sjf ::.. (somebody_at_unknown.org)
Date: 10/11/04
- Next message: Bernhard Herzog: "Re: Why can't I xor strings?"
- Previous message: Fredrik Lundh: "Re: End of file"
- In reply to: Duncan Booth: "Re: define 'in' operator on lists"
- Next in thread: Bengt Richter: "Re: define 'in' operator on lists"
- Reply: Bengt Richter: "Re: define 'in' operator on lists"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 11 Oct 2004 12:51:57 +0200
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
-- .:: sjf ::.. "Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
- Next message: Bernhard Herzog: "Re: Why can't I xor strings?"
- Previous message: Fredrik Lundh: "Re: End of file"
- In reply to: Duncan Booth: "Re: define 'in' operator on lists"
- Next in thread: Bengt Richter: "Re: define 'in' operator on lists"
- Reply: Bengt Richter: "Re: define 'in' operator on lists"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|