define 'in' operator on lists

S

..:: sjf ::..

Hi all,

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. By way of example:

L = [1, 2, 3, 4, 5, 6, 7]
_list = [3, 4, 5]

if L2 in L0:
return True
else:
return False

How to do that?
 
D

Duncan Booth

...:: sjf ::.. said:
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:
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) True
containedinsequence([3, 4, 7], L) False
containedinsequence([3, 4, 3], L) False
containedinsequence([], L) True
 
S

..:: sjf ::..

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:


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
 
P

Phil Frost

Hi all,

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. By way of example:

L = [1, 2, 3, 4, 5, 6, 7]
_list = [3, 4, 5]

if L2 in L0:
return True
else:
return False

How to do that?

You might find that the 'sets' module does what you need.
 
B

Bengt Richter

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 ;-)
... if not sub: return True
... sub0 = sub[0]
... start = 0
... lensub = len(sub)
... while True:
... try: start = seq.index(sub0, start)
... except ValueError: return False
... if seq[start:start+lensub] == sub: return True
... start +=1
... return False
...
>>> L = [1, 2, 3, 4, 5, 6]
>>> issubseq([3,4,5],L) True
>>> issubseq([3,4,6],L) False
>>> issubseq([3],L) True
>>> issubseq([],L) True
>>> issubseq([6],L) True
>>> issubseq([1,2],L)
True

Regards,
Bengt Richter
 
A

Aaron Bingham

Phil said:
Hi all,

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. By way of example:

L = [1, 2, 3, 4, 5, 6, 7]
_list = [3, 4, 5]

if L2 in L0:
return True
else:
return False

How to do that?

You might find that the 'sets' module does what you need.
The OP said he cares about order, so sets will certainly *not* do what
he wants. I suggest looking up string matching algorithms. This is
effectively what has been asked for but for strings of numbers instead
of strings of characters.

Aaron
 
S

..:: sjf ::..

pewnego dnia niejaki(a) Bengt Richter wstukal(a) byl(a) co nastepuje...:
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!
 
M

Michael Hoffman

Phil said:
[sjf]
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. By way of example:
You might find that the 'sets' module does what you need.

Those sets are not ordered.
 
M

Michael Hoffman

Peter said:
Relax. It's only shadowed inside a function that doesn't use the builtin
anyway.

I can see that, but I was thinking mainly in terms of avoiding warnings and problems if the Python developers eventually try to optimize references to the builtins :)
 
P

Peter Otten

Michael said:
I can see that, but I was thinking mainly in terms of avoiding warnings
and problems if the Python developers eventually try to optimize
references to the builtins :)

I can see how assigning to a global can cause trouble here, but a local
should be easy to resolve at compile time as 'not the builtin' and
therefore not hinder optimization.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top