且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查对象是否是python中的列表列表?

更新时间:2022-03-09 06:21:01

使用>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.