Python:确定“列表列表"中是否包含未排序的列表,而与元素的顺序无关 [英] Python: Determine if an unsorted list is contained in a 'list of lists', regardless of the order to the elements
问题描述
I have a similar question to this question: Determine if 2 lists have the same elements, regardless of order?
确定列表列表" myListOfLists
中是否包含未排序列表list1
的最佳/最快方法是什么,而不管list1
中元素的顺序如何?我的尝试被包装在函数doSomething(...)
中,该函数多次调用:
What is the best/quickest way to determine whether an unsorted list list1
is contained in a 'list of lists' myListOfLists
, regardless of the order to the elements in list1
? My attempt is wrapped up in the function doSomething(...)
which I call many times:
def doSomething(myListOfLists, otherInputs):
list1 = []
... # do something here with `otherInputs'
... # which gives `list1' some values
# now only append `list1' to `myListOfLists' if it doesn't already exist
# and if it does exist, remove it
removeFromList = False
for myList in myListOfLists:
if sorted(list1) == sorted(myList):
removeFromList = True
break
if removeFromList:
myListOfLists.remove(list1)
else:
myListOfLists.append(list1)
return myListOfLists
此问题是我需要运行功能doSomething(...)
大约1.0e5次.随着myListOfLists
在每次调用doSomething(...)
时变大,这变得非常耗时.
The problem with this is that I need to run the function doSomething(...)
approximately 1.0e5 times. As myListOfLists
gets bigger with every call of doSomething(...)
this becomes massively time consuming.
对任务进行了一些澄清.让我在此处给出所需输出的示例:
Some clarification of the task. Let me give an example of the desired output here:
a = []
doSomething(a, [1,2,3])
>> a = [1,2,3]
由于[1,2,3]
不在a
中,因此将其附加到a
.
Because [1,2,3]
is not in a
, it is appended to a
.
doSomething(a, [3,4,5])
>> a = [[1,2,3], [3,4,5]]
由于[3,4,5]
不在a
中,因此将其附加到a
.
Because [3,4,5]
is not in a
, it is appended to a
.
doSomething(a, [1,2,3])
>>[3,4,5]
由于a
中的[1,2,3]
是,因此已将其从a
中删除.
Because [1,2,3]
is in a
, it is removed from a
.
所有列表的长度相同.
推荐答案
您可以在此处使用设置:
You can use sets here:
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
完全相同的项目.
Update 1: Any Sublist contains exactly same items as 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
是任何子列表的子集:
Update 2: otherInputs
is a subset of any of the sublist:
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
这篇关于Python:确定“列表列表"中是否包含未排序的列表,而与元素的顺序无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!