简体   繁体   English

如何查看列表是否包含Python中的另一个列表?

[英]How can I see if a list contains another list in Python?

How would I check to see if a list contains other lists? 我如何检查列表是否包含其他列表? I need it so that 我需要它

[['cow'], 12, 3, [[4]]]

would output True , while something like 将输出True ,而类似的东西

['cow', 12, 3, 4]

would output False . 会输出False

If you also want to find subclasses of lists then you should use isinstance : 如果您还想查找列表的子类,那么您应该使用isinstance

def any_list_in(obj):
    return any(isinstance(item, list) for item in obj)

any stops as soon as the condition is True so this only needs to check only as many items as necessary. 条件为True any停止,因此只需要检查所需的项目数。

>>> any_list_in([['cow'], 12, 3, [[4]]])
True

>>> any_list_in(['cow', 12, 3, 4])
False

The isinstance(item, list) for item in obj is a generator expression that works similar to a for -loop or a list-comprehension. isinstance(item, list) for item in objisinstance(item, list) for item in obj是一个生成器表达式 ,其工作方式类似于for -loop或list-comprehension。 It could also be written as (longer and slightly slower but maybe that's better to comprehend): 它也可以写成(更长和稍慢但可能更好理解):

def any_list_in(obj):
    for item in obj:
        if isinstance(item, list):
            return True
    return False

Here's a neat solution using a list comprehension. 这是一个使用列表理解的简洁解决方案。

Given obj = [['cow'], 12, 3, [[4]]] , we'll first use a list comprehension to get a list of types in obj : 给定obj = [['cow'], 12, 3, [[4]]] ,我们将首先使用列表推导来获取obj中的类型列表:

>>> [type(x) for x in obj]
[<type 'list'>, <type 'int'>, <type 'int'>, <type 'list'>]

Now, we simply check if list is in the list of types we created. 现在,我们只需检查list是否在我们创建的类型列表中。 Let's revisit our list comprehension and turn it into a boolean expression: 让我们重新审视列表理解并将其转换为布尔表达式:

>>> list in [type(x) for x in obj]

There, that's nice and short. 那里,这很好,很短。 So does it work? 它有用吗?

>>> obj = [['cow'], 12, 3, [[4]]]
>>> list in [type(x) for x in obj]
True
>>> obj = ['cow', 12, 3, 4]
>>> list in [type(x) for x in obj]
False

You can use this method if your list does not contain some string with '[' int it . 如果列表中没有包含'['int'的字符串,则可以使用此方法。

def check(a):
   a=str(a)
   if a.count('[')==1:  # or a.count(']')==1;
      return False
   return True

In python 2.7 we have a module for this work:( I don't know about any such module in python 3.x ) 在python 2.7中我们有一个模块用于这项工作:(我不知道python 3.x中的任何这样的模块)

from compiler.ast import flatten    
def check(a):
    if flatten(a)==a:
        return False
    return True

暂无
暂无

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

相关问题 如何测试列表是否包含Python中具有特定项的另一个列表? - How can I test if a list contains another list with particular items in Python? 如何检查列表中一行的任何部分是否包含另一个列表的整行? PYTHON - How can I check if any part of a line from a list contains the full line of another list? PYTHON 在 Python 中,如何检查一个数组是否包含另一个数组/列表的所有元素,包括重复项? - In Python, how can I check if an array contains all elements of another array/list, including duplicates? 如何查看python可用导入的列表? - How can I see a list of available imports for python? 如何将包含字典列表的文件读入python? - How can I read a file that contains a list of dictionaries into python? 如何在Python中创建包含各种类型元素的列表? - How can I make a list that contains various types of elements in Python? 如何查看一个字符串是否包含列表中的所有子字符串? [蟒蛇] - How to see if a string contains all substrings from a list? [Python] 如何通过知道元组中的值来查看元组列表是否包含特定元组? - How can I see if a list of tuples contains a specific tuple by knowing the values in this tuple? 如何将python列表转换为其他列表格式 - How can i convert my python list to another list format 如何在 python 的另一个列表中插入一个列表的 position? - How can I insert a position of a list in another list in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM