[英]Python - Find a list within a list of lists
I have a list of lists, where each list may be of arbitrary length for example: 我有一个列表列表,其中每个列表都可以是任意长度,例如:
list_of_lists = [[2, 2, 2, 3], [2, 3, 4], [2, 2, 6]]
I need a way to search that list of lists for a specific list. 我需要一种在列表列表中搜索特定列表的方法。 For example, a contains()
function which would return True
below: 例如,一个contains()
函数将在下面返回True
:
list_of_lists.contains([2, 2, 6])
I have seen answers where the "inner lists" can be converted into tuples, but that doesn't help me here. 我已经看到了可以将“内部列表”转换为元组的答案,但这对我没有帮助。 Is there a library out there that has this function? 是否有一个具有此功能的库?
Using in
: in
使用:
list_of_lists = [[2,2,2,3],[2,3,4],[2,2,6]]
if [2,2,6] in list_of_lists:
print("found")
else:
print("not found")
OUTPUT : 输出 :
found
Similarly, let's say the last list in the nested list was: [2, 2, 6, 8]
同样,假设嵌套列表中的最后一个列表为: [2, 2, 6, 8]
list_of_lists = [[2,2,2,3],[2,3,4],[2,2,6,8]]
if [2,2,6] in list_of_lists:
print("found")
else:
print("not found")
OUTPUT : 输出 :
not found
EDIT : 编辑 :
While I was at it, if you wanted to have a boolean value for the existance of the list: 在此期间,如果您想为列表的存在提供布尔值:
def chkList(lst):
return True if lst in list_of_lists else False
list_of_lists = [[2,2,2,3],[2,3,4],[2,2,6]]
print(chkList([2,2,6]))
OUTPUT : 输出 :
True
Use in
: 使用in
:
print([2,2,6] in list_of_lists)
Or use
__contains__
:
或使用
__contains__
:
print(list_of_lists.__contains__([2,2,6]))
(DON'T USE __contains__
, HIGHLY UN-RECOMENDED) (请勿使用__contains__
,高度不推荐)
There are multiple answers suggesting to use in
or ==
to see if the list contains the element (another list). 建议in
或==
使用多个答案,以查看列表是否包含元素(另一个列表)。
However, if you do not care about the order of the elements in the lists you are comparing, here is a solution to that. 但是,如果您不关心要比较的列表中元素的顺序,则可以采用以下解决方案。
import collections
# 'list_' is the list we are searching for in a bigger list 'list_of_lists'
for element in list_of_lists:
if collections.Counter(element) == collections.Counter(list_) :
return True
The above solution requires the elements to be hashable. 以上解决方案要求元素是可哈希的。
If you find collections
too complicated to understand, you can simply use set(list_) == set(element)
. 如果发现collections
太复杂而难以理解,则可以简单地使用set(list_) == set(element)
。
The above methods require elements to be hashable. 以上方法要求元素是可哈希的。 If the elements are not hashable but sortable, you can use sorted(list_)==sorted(element)
. 如果元素不可哈希但可排序,则可以使用sorted(list_)==sorted(element)
。
What if you dosomething like 如果您喜欢做些什么
if any(list == [2, 2, 6] for list in list_of_lists):
#execute whatever code you want for this case
Just in case you needed a more generic solution checking if your target list is actually an ordered sublist of one of inner lists. 万一您需要一个更通用的解决方案,以检查您的目标列表是否实际上是内部列表之一的有序子列表。 The trick would be converting the list into a string representation that allows efficient substring checking. 技巧是将列表转换为允许有效子字符串检查的字符串表示形式。
>>> list_of_lists = [[2,2,2,3],[2,3,4],[2,2,6,8]]
>>> serialize = lambda x: ','.join(map(str,x))
>>> any(serialize([2,2,6]) in serialize(item) for item in list_of_lists)
True
>>> any(serialize([2,2,7]) in serialize(item) for item in list_of_lists)
False
Please check the following code from ipython 请从ipython检查以下代码
In [18]: list
Out[18]: [[1, 2, 3], [4, 5, 6], [3, 2, 4]]
In [19]: def contains(sublist):
if sublist in list:
return True
else:
return False
....:
In [20]: contains([1,2,3])
True
In [21]: contains(2)
False
In [22]:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.