简体   繁体   English

如何检查列表列表中的子列表?

[英]How to check for a sublist in a list of lists?

Let's say I have this code: 假设我有以下代码:

lst1 = [['1','g','8','c','p'],['lion','tiger','bear','oh','my'],['true','false','maybe','g','8']]
lst2 = ['g','8']

How would I check if lst2 is in lst1? 我如何检查lst2是否在lst1中?

If I understood the question correctly you can just use set and any : 如果我正确理解了这个问题,则可以使用setany

any(set(lst2) <= set(x) for x in lst1)

where set(lst2) <= set(x) checks if lst2 is sublist of x . 其中set(lst2) <= set(x)检查lst2是否是x子列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查列表是否包含来自另一个列表子列表的项目? [等候接听] - How to check if a list contains items from another lists sublist? [on hold] 如何检查字符串是否在列表列表中,如果有则返回子列表? - How do I check if a string is in a list of lists, and return the sublist if it is? 如何将列表的子列表与同一列表中的其他列表的子列表进行比较 - How to compare sublist of list with sublist of other list that are in same list of lists 如何按浮动子列表的项目对列表列表进行排序 - How to sort a list of lists by an item of the sublist that is a float 我有一个包含 boolean 个值的列表列表。 如何输入 boolean function 并在列表的每个子列表中检查它? - I have a list of lists containing boolean values. How can I input a boolean function and check it in every sublist of my list? 在列表列表中替换垂直子列表 - Replacing a vertical sublist in a list of lists 使用python加入列表列表中的子列表 - join sublist in a list of lists with python 如何通过子列表项子集在Python列表列表中删除重复项? - How to remove duplicates, by sublist item subset, in a list of lists in Python? 检查列表列表中的列表元素并返回子列表 - Checking if list element in list of lists and returning sublist 检查字符串是否是列表的有序子列表 - Check if a string is a ordered sublist of a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM