473,574 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1829
...:: 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 containedinsequ ence(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]
containedinsequ ence([3, 4, 6], L) True containedinsequ ence([3, 4, 7], L) False containedinsequ ence([3, 4, 3], L) False containedinsequ ence([], 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 containedinsequ ence(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]
containedin sequence([3, 4, 6], L)


OK, this is nearly what I am expecting, but I want if
containedsequen ce([3, 4, 5], L) returns True, but
containedsequen ce([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******@unkno wn.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 containedinsequ ence(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]
>containedi nsequence([3, 4, 6], L)


OK, this is nearly what I am expecting, but I want if
containedseque nce([3, 4, 5], L) returns True, but
containedseque nce([3, 4, 6], L) returns False
because that sequence not exist in longlist exactly

Not very tested (just what you see ;-)
def issubseq(sub,se q): ... 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+len sub] == 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******@unkno wn.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 containedinsequ ence(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]
>>contained insequence([3, 4, 6], L)

OK, this is nearly what I am expecting, but I want if
containedsequ ence([3, 4, 5], L) returns True, but
containedsequ ence([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 containedinsequ ence(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 containedinsequ ence(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

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

Similar topics

699
33577
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 capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it...
5
2376
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( size_t size ); void *operator new( size_t size, AllocationType allocType ); void *operator new( size_t size, MemoryHandler* memHandler ); void *operator...
73
4570
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 endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix...
7
3038
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 surely this is possible? I could define an operator<< but for various reasons, I really want to convert to a 'const char*' (to embed into a...
0
7799
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8226
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7806
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8094
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5612
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3739
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1053
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.