How to Test If a List Contains Another List as a Contiguous Subsequence

How to test if a list contains another list as a contiguous subsequence?

Here is my version:

def contains(small, big):
for i in xrange(len(big)-len(small)+1):
for j in xrange(len(small)):
if big[i+j] != small[j]:
break
else:
return i, i+len(small)
return False

It returns a tuple of (start, end+1) since I think that is more pythonic, as Andrew Jaffe points out in his comment. It does not slice any sublists so should be reasonably efficient.

One point of interest for newbies is that it uses the else clause on the for statement - this is not something I use very often but can be invaluable in situations like this.

This is identical to finding substrings in a string, so for large lists it may be more efficient to implement something like the Boyer-Moore algorithm.

Note: If you are using Python3, change xrange to range.

LINQ List Contains Another List (Contiguous)

public static bool Contains<T>(this IEnumerable<T> haystack, IEnumerable<T> needle)
{
var hayList = haystack.ToList();
var needleList = needle.ToList();
return Enumerable.Range(0, hayList.Count)
.Select(start => hayList.Skip(start).Take(needleList.Count))
.Any( subsequence => subsequence.SequenceEqual(needleList));
}

How to determine if one list contains another?

If there were no duplicates, or duplicates didn't matter (that is, if your l1 and l3 were both supersets of each other), you'd just use sets. But since if you want l1 to be a proper superset of l3, you're talking about multisets. Fortunately, Counter already implements multisets for you:

from collections import Counter
def is_superset(a, b):
return not Counter(b) - Counter(a)

Notice that this - is proper multiset difference between multisets (just as - is proper set difference between sets), not an elementwise subtraction across dicts. So if you subtract a super(multi)set, you get an empty multiset (that is, Counter()—which is, like all empty collections in Python, falsey).

So now:

>>> is_superset(l1, l2)
True
>>> is_superset(l1, l3)
True
>>> is_superset(l1, l4)
False
>>> is_superset(l1, l5)
False

Plus:

>>> is_superset(l3, l1)
False

Checking if a lists' items are in the same order in another list

After looking at the question I wrote this. Unlike some of the others that assume that the elements in the first array must occur in the second contiguously, this does not.

def in_order(a,b):
j = iter(b)
for i in a:
while True:
try:
j_ = j.next()
except StopIteration:
return False
if i == j_:
break
return True

The OP's example:

a = [1, 5, 2, 6, 2, 5, 1]
b = [4, 1, 5, 2, 6, 2, 5, 1, 6, 2, 3]
print in_order(a,b)

prints: True

How can i know if it has list in list in python

list1 = [1,2,3]
list2 = [1, 2, 3, 4, 5]

result = all(elem in list2 for elem in list1)

print(result)# True

how to check if a list in the other one in python

As your variable names already suggested, you may use all(...):

numbers = [1, 25, 31, 42, 45, 52, 59, 63, 66, 70]
allnumbers = [1, 2, 9, 25, 26, 30, 31, 35, 42, 49, 45, 51, 52, 55, 59, 60, 63, 65, 66, 70]

if (all(number in allnumbers for number in numbers)):
print("Yes")

Another variant would include using sets:

smaller_list = set(numbers)
bigger_list = set(allnumbers)

if biggerlist.intersection(smaller_list) == smaller_list:
print("yes")

To see, how many numbers have matched, use:

numbers_matched = len(bigger_list.intersection(smaller_list))

See a demo on ideone.com.

Find the indices of list items in another list only when consecutive

Keep a sliding window of length equal to the length of listB

for i in range(len(listA)- len(listB) + 1):
if listA[i:i+len(listB)] == listB:
print([k for k in range(i,i+len(listB))])

Checking if list is a sublist

i need to check if list1 is a sublist to list2 (True; if every integer in list2 that is common with list1 is in the same order of indexes as in list1)

Your code isn't working because as soon as a list element in ls1 doesn't occur in ls2 it will return False immediately.

This creates two lists that contain only the common elements (but in their original order) and then returns True when they are the same:

def sublist(lst1, lst2):
ls1 = [element for element in lst1 if element in lst2]
ls2 = [element for element in lst2 if element in lst1]
return ls1 == ls2

edit: A memory-efficient variant:

def sublist(ls1, ls2):
'''
>>> sublist([], [1,2,3])
True
>>> sublist([1,2,3,4], [2,5,3])
True
>>> sublist([1,2,3,4], [0,3,2])
False
>>> sublist([1,2,3,4], [1,2,5,6,7,8,5,76,4,3])
False
'''
def get_all_in(one, another):
for element in one:
if element in another:
yield element

for x1, x2 in zip(get_all_in(ls1, ls2), get_all_in(ls2, ls1)):
if x1 != x2:
return False

return True


Related Topics



Leave a reply



Submit