Re: testing if a list contains a sublist

2011-08-20 Thread Simon Forman
On Mon, Aug 15, 2011 at 4:26 PM, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is

Re: testing if a list contains a sublist

2011-08-19 Thread Steven D'Aprano
Johannes wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? [...] For anyone interested, here's a pair of functions that implement sub-sequence testing similar to str.find and str.rfind:

Re: testing if a list contains a sublist

2011-08-17 Thread Nobody
On Tue, 16 Aug 2011 09:57:57 -0400, John Posner wrote: How about using Python's core support for == on list objects: for i in range(alist_sz - slist_sz + 1): if slist == alist[i:i+slist_sz]: return True This is bound to be asymptotically O(alist_sz * slist_sz), even

Re: testing if a list contains a sublist

2011-08-17 Thread Ameretat Reith
On Se shanbe 25 Mordad 1390 01:26:54 Johannes wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained

Re: testing if a list contains a sublist

2011-08-16 Thread ChasBrown
On Aug 15, 4:26 pm, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained

Re: testing if a list contains a sublist

2011-08-16 Thread ChasBrown
On Aug 15, 4:26 pm, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained

Re: testing if a list contains a sublist

2011-08-16 Thread ChasBrown
On Aug 15, 4:26 pm, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained

Re: testing if a list contains a sublist

2011-08-16 Thread Laszlo Nagy
hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2 l1 = [1,2,3], l2 = [1,3,5,7] - l1 is not

Re: testing if a list contains a sublist

2011-08-16 Thread alex23
Laszlo Nagy gand...@shopzeus.com wrote: Fastest, error-free and simplest solution is to use sets:   l1 = [1,2]   l2 = [1,2,3,4,5]   set(l1)-set(l2) set([])   set(l2)-set(l1) set([3, 4, 5])   Error-free? Not given the stated requirements: l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not

Re: testing if a list contains a sublist

2011-08-16 Thread alex23
On Aug 16, 4:51 pm, Laszlo Nagy gand...@shopzeus.com wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] -  l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] -  l1 is

Re: testing if a list contains a sublist

2011-08-16 Thread ChasBrown
On Aug 15, 11:51 pm, Laszlo Nagy gand...@shopzeus.com wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] -  l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] -  l1 is

Re: testing if a list contains a sublist

2011-08-16 Thread Peter Otten
Johannes wrote: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2 l1 = [1,2,3], l2 = [1,3,5,7] - l1 is

Re: testing if a list contains a sublist

2011-08-16 Thread Laszlo Nagy
Error free? Consider this stated requirement: l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2 If you look it the strict way, containment relation for lists is meant this way: l1 = [] l2 = [1,l1,2] # l2 CONTAINS l1 But you are right, I was wrong. So let's clarify what the OP

Re: testing if a list contains a sublist

2011-08-16 Thread Steven D'Aprano
On Tue, 16 Aug 2011 12:12 pm Steven D'Aprano wrote: On Tue, 16 Aug 2011 09:26 am Johannes wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? This is not the most efficient algorithm, but for short lists it should be

Re: testing if a list contains a sublist

2011-08-16 Thread Johannes
Am 16.08.2011 09:44, schrieb Peter Otten: Johannes wrote: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in

Re: testing if a list contains a sublist

2011-08-16 Thread Steven D'Aprano
On Tue, 16 Aug 2011 04:14 pm ChasBrown wrote: On Aug 15, 4:26 pm, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 =

Re: testing if a list contains a sublist

2011-08-16 Thread Peter Otten
Johannes wrote: Am 16.08.2011 09:44, schrieb Peter Otten: Johannes wrote: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is

Re: testing if a list contains a sublist

2011-08-16 Thread Nobody
On Tue, 16 Aug 2011 01:26:54 +0200, Johannes wrote: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? Best is subjective. AFAIK, the theoretically-optimal algorithm is Boyer-Moore. But that would require a fair amount of code, and

Re: testing if a list contains a sublist

2011-08-16 Thread Alain Ketterlin
Roy Smith r...@panix.com writes: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? [...] import re def sublist(l1, l2): s1 = ''.join(map(str, l1)) s2 = ''.join(map(str, l2)) return re.search(s1, s2) This is complete

Re: testing if a list contains a sublist

2011-08-16 Thread Roy Smith
In article 8739h18rzj@dpt-info.u-strasbg.fr, Alain Ketterlin al...@dpt-info.u-strasbg.fr wrote: Roy Smith r...@panix.com writes: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? [...] import re def sublist(l1, l2):

Re: testing if a list contains a sublist

2011-08-16 Thread John Posner
On 2:59 PM, Nobody wrote: On Tue, 16 Aug 2011 01:26:54 +0200, Johannes wrote: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? Best is subjective. AFAIK, the theoretically-optimal algorithm is Boyer-Moore. But that would require a

Re: testing if a list contains a sublist

2011-08-16 Thread John Posner
On 2:59 PM, Nobody wrote: On Tue, 16 Aug 2011 01:26:54 +0200, Johannes wrote: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? Best is subjective. AFAIK, the theoretically-optimal algorithm is Boyer-Moore. But that would require a

Re: testing if a list contains a sublist

2011-08-16 Thread nn
On Aug 16, 8:23 am, Alain Ketterlin al...@dpt-info.u-strasbg.fr wrote: Roy Smith r...@panix.com writes: what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? [...] import re def sublist(l1, l2):     s1 = ''.join(map(str, l1))  

Re: testing if a list contains a sublist

2011-08-16 Thread Laszlo Nagy
That can be easily fixed: def sublist(lst1, lst2): s1 = ','.join(map(str, lst1)) s2 = ','.join(map(str, lst2)) return False if s2.find(s1)==-1 else True I don't know about best, but it works for the examples given. For numbers, it will always work. But what about

Re: testing if a list contains a sublist

2011-08-16 Thread Johannes
Am 16.08.2011 10:00, schrieb Laszlo Nagy: Error free? Consider this stated requirement: l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2 If you look it the strict way, containment relation for lists is meant this way: l1 = [] l2 = [1,l1,2] # l2 CONTAINS l1 But you are

Re: testing if a list contains a sublist

2011-08-16 Thread Alain Ketterlin
Laszlo Nagy gand...@shopzeus.com writes: def sublist(lst1, lst2): s1 = ','.join(map(str, lst1)) s2 = ','.join(map(str, lst2)) return False if s2.find(s1)==-1 else True I don't know about best, but it works for the examples given. For numbers, it will always work. I'm not

Re: testing if a list contains a sublist

2011-08-16 Thread MRAB
On 16/08/2011 00:26, Johannes wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2 l1 = [1,2,3],

Re: testing if a list contains a sublist

2011-08-16 Thread Neil Cerutti
On 2011-08-16, nn prueba...@latinmail.com wrote: That can be easily fixed: def sublist(lst1, lst2): s1 = ','.join(map(str, lst1)) s2 = ','.join(map(str, lst2)) return False if s2.find(s1)==-1 else True sublist([1,2,3],[1,2,3,4,5]) True sublist([1,2,2],[1,2,3,4,5])

Re: testing if a list contains a sublist

2011-08-16 Thread ChasBrown
On Aug 16, 1:37 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: On Tue, 16 Aug 2011 04:14 pm ChasBrown wrote: On Aug 15, 4:26 pm, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second

testing if a list contains a sublist

2011-08-15 Thread Johannes
hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 = [1,2,2,], l2 = [1,2,3,4,5] - l1 is not contained in l2 l1 = [1,2,3], l2 = [1,3,5,7] - l1 is not contained in

Re: testing if a list contains a sublist

2011-08-15 Thread Dan Stromberg
Check out collections.Counter if you have 2.7 or up. If you don't, google for multiset or bag types. On Mon, Aug 15, 2011 at 4:26 PM, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for

Re: testing if a list contains a sublist

2011-08-15 Thread Roy Smith
In article mailman.27.1313450819.27778.python-l...@python.org, Johannes dajo.m...@web.de wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? for example: l1 = [1,2], l2 = [1,2,3,4,5] - l1 is contained in l2 l1 =

Re: testing if a list contains a sublist

2011-08-15 Thread Steven D'Aprano
On Tue, 16 Aug 2011 09:26 am Johannes wrote: hi list, what is the best way to check if a given list (lets call it l1) is totally contained in a second list (l2)? This is not the most efficient algorithm, but for short lists it should be plenty fast enough: def contains(alist, sublist):