本文介绍了检查对象是否是python中的列表列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 type(obj)-> list 来检测对象是否在python中的列表中.

Is there any ways we can detect if an object is list in python using type(obj) --> list.

但是我们如何检测对象是否为表单列表:

But how can we detect if the object is list of list of the form as:

[['a','b']['a','b'][][]]

推荐答案

使用>code> isinstance() 检查特定类型:

Use isinstance() to check for a specific type:

>>> isinstance([], list)
True

使用 all() 来测试 all 元素是否属于某种类型:

Use all() to test if all elements are of a certain type:

all(isinstance(elem, list) for elem in list_of_lists)

all()短路;如果任何测试返回 False ,则循环终止,并返回 False .仅当一个元素之外的所有元素都返回 True 时, all()才需要检查可迭代对象的每个元素.

all() short-circuits; if any of the tests returns False, the loop is terminated and False is returned. Only if all but one element returns True does all() need to examine every element of the iterable.

这篇关于检查对象是否是python中的列表列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:33
查看更多