473,387 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

define 'in' operator on lists

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?

--
..:: sjf ::..
"Linux is like Wigwam. No gates, no windows... Apache inside ;-)"
Jul 18 '05 #1
11 1816
...:: 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) True containedinsequence([3, 4, 7], L) False containedinsequence([3, 4, 3], L) False containedinsequence([], L) True

Jul 18 '05 #2
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 ;-)"
Jul 18 '05 #3
On Mon, Oct 11, 2004 at 11:53:36AM +0200, ..:: sjf ::.. wrote:
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.
Jul 18 '05 #4
On Mon, 11 Oct 2004 12:51:57 +0200, "..:: sjf ::.." <so******@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 ;-)
def issubseq(sub,seq): ... 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
Jul 18 '05 #5
Phil Frost wrote:
On Mon, Oct 11, 2004 at 11:53:36AM +0200, ..:: sjf ::.. wrote:

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

Jul 18 '05 #6
pewnego dnia niejaki(a) Bengt Richter wstukal(a) byl(a) co nastepuje...:
On Mon, 11 Oct 2004 12:51:57 +0200, "..:: sjf ::.." <so******@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 ;-)"
Jul 18 '05 #7
Duncan Booth wrote:
def containedinsequence(short, long):


It's probably a good idea not to shadow the long built-in.
--
Michael Hoffman
Jul 18 '05 #8
Phil Frost wrote:
[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.
--
Michael Hoffman
Jul 18 '05 #9
Michael Hoffman wrote:
Duncan Booth wrote:
>def containedinsequence(short, long):


It's probably a good idea not to shadow the long built-in.


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

Peter

PS: For me to make that U-turn it took GvR pointing out the obvious:
http://mail.python.org/pipermail/pyt...ly/045948.html
Jul 18 '05 #10
Peter Otten wrote:
Michael Hoffman wrote:
It's probably a good idea not to shadow the long built-in.


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 :)

--
Michael Hoffman
Jul 18 '05 #11
Michael Hoffman wrote:
Peter Otten wrote:
Michael Hoffman wrote:
It's probably a good idea not to shadow the long built-in.


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 :)


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
Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.