I have a iterator of int's coming from a table lookup, and need to check if their multiset is contained in a given fixed "multiset" ms
. Currently, I sort ms
in the beginning, and then sort the int's in my iterator and check the multiset containment (of sorted lists) as follows:
vals = sorted(my_vals)
for it in ... :
test_vals = sorted( i for i in it )
if is_sublist(test_vals,vals):
# do stuff
where
def is_sublist(L1,L2):
m = len(L1)
n = len(L2)
i = j = 0
while j <= n:
if i == m:
return True
elif j == n:
return False
a,b = L1[i], L2[j]
if a == b:
i += 1
j+= 1
elif a > b:
j += 1
else:
return False
- Usually, my lists are rather short (1--20 elements)
- I tried to use
Counter
, but the time disadvantage of its initialization is worse than the time advantage of the containment test. - I do this ~10^6 times, so I should maybe do it in
cython
Any ideas or pointers would be nice -- Thanks! (sorry for clicking the post button too early first...)