您可以在此处使用集合:
def doSomething(myListOfLists, otherInputs):
s = set(otherInputs) #create set from otherInputs
for item in myListOfLists:
#remove the common items between `s` and current sublist from `s`.
s -= s.intersection(item)
#if `s` is empty, means all items found. Return True
if not s:
return True
return not bool(s)
...
>>> doSomething([[1, 2, 7],[6, 5, 4], [10, 9, 10]], [7, 6, 8])
False
>>> doSomething([[1, 2, 7],[6, 5, 4], [10, 8, 10]], [7, 6, 8])
True
更新1:任何子列表包含完全相同的项目otherInputs
.
def doSomething(myListOfLists, otherInputs):
s = set(otherInputs)
return any(set(item) == s for item in myListOfLists)
...
>>> doSomething([[6, 8, 7],[6, 5, 4], [10, 8, 10]], [7, 6, 8])
True
>>> doSomething([[1, 2, 7],[6, 5, 4], [10, 8, 10]], [7, 6, 8])
False
更新2: otherInputs
是任何子列表的子集:
def doSomething(myListOfLists, otherInputs):
s = set(otherInputs)
return any(s.issubset(item) for item in myListOfLists)
...
>>> doSomething([[6, 8, 7],[6, 5, 4], [10, 8, 10]], [7, 6, 8])
True
>>> doSomething([[6, 8, 7, 10],[6, 5, 4], [10, 8, 10]], [7, 6, 8])
True
>>> doSomething([[1, 2, 7],[6, 5, 4], [10, 8, 10]], [7, 6, 8])
False