Issue
#sub list test 1
a = [1,1,2]
b = [0, 1,1,1,2,1]
#sub list test 2
c = [4,5]
d = [2,3,4,5,6]
if all(i in a for i in b):
print("Yes I have all the elements but not sure about their order :( ")
I have tried all()
or using counter()
from collections
module. I can't seem to figure it out how to check their order too.
Solution
This will check all subList of length a and return True if a is found in b
Not sure about order :
def isSublist(a,b):
#checking all unique elements and their count
for i in set(a):
if i not in b or a.count(i) > b.count(i):
return False
return True
Sure about order :
def isSublist(a,b):
for i in range(len(b)-len(a)+1):
if b[i:i+len(a)] == a:
return True
return False
Answered By - THUNDER 07
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.