On Mon, Aug 15, 2011 at 4:26 PM, 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 contai
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:
http://code.activestate.com/r
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 co
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),
On Aug 16, 1:37 am, Steven D'Aprano wrote:
> On Tue, 16 Aug 2011 04:14 pm ChasBrown wrote:
>
>
>
> > On Aug 15, 4:26 pm, 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
On 2011-08-16, nn 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])
> False
>>
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]
Laszlo Nagy 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 even
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
>
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
l
On Aug 16, 8:23 am, Alain Ketterlin
wrote:
> Roy Smith 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
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 req
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 req
In article <8739h18rzj@dpt-info.u-strasbg.fr>,
Alain Ketterlin wrote:
> Roy Smith 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(s
Roy Smith 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 nonsen
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 Py
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,], l
On Tue, 16 Aug 2011 04:14 pm ChasBrown wrote:
> On Aug 15, 4:26 pm, 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,
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 no
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
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
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] ->
On Aug 15, 11:51 pm, Laszlo Nagy 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
On Aug 16, 4:51 pm, Laszlo Nagy 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
Laszlo Nagy 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
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 conta
On Aug 15, 4:26 pm, 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
> l
On Aug 15, 4:26 pm, 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
> l
On Aug 15, 4:26 pm, 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
> l
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):
In article ,
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 =
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 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
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 i
33 matches
Mail list logo