If we have the following list:
如果我们有以下清单:
list = ['UMM', 'Uma', ['Ulaster','Ulter']]
If I need to find out if an element in the list is itself a list, what can I replace aValidList in the following code with?
如果我需要找出列表中的元素是否本身就是一个列表,我可以用以下代码替换aValidList吗?
for e in list:
if e == aValidList:
return True
Is there a special import to use? Is there a best way of checking if a variable/element is a list?
是否有特殊的导入要使用?是否有最好的方法检查变量/元素是否是列表?
3 个解决方案
#1
98
Use isinstance
:
使用isinstance:
if isinstance(e, list):
If you want to check that an object is a list or a tuple, pass several classes to isinstance
:
如果要检查对象是list或tuple,请将几个类传递给isinstance:
if isinstance(e, (list, tuple)):
#2
20
-
Work out what specific properties of a
list
you want the items to have. Do they need to be indexable? Sliceable? Do they need an.append()
method?计算出你想要的条目的具体属性。它们需要可索引吗?切割的?他们需要一个.append()方法吗?
-
Look up the abstract base class which describes that particular type in the
collections
module.查找描述集合模块中特定类型的抽象基类。
-
Use
isinstance
:使用isinstance:
isinstance(x, collections.MutableSequence)
You might ask "why not just use type(x) == list
?" You shouldn't do that, because then you won't support things that look like lists. And part of the Python mentality is duck typing:
您可能会问“为什么不使用type(x) = list?”你不应该这样做,因为那样你就不会支持看起来像列表的东西。Python思维的一部分是鸭子类型:
I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck
我看见一只鸟像鸭子一样走路,像鸭子一样游泳,像鸭子一样嘎嘎叫,我把那只鸟叫做鸭子
In other words, you shouldn't require that the objects are list
s, just that they have the methods you will need. The collections
module provides a bunch of abstract base classes, which are a bit like Java interfaces. Any type that is an instance of collections.Sequence
, for example, will support indexing.
换句话说,您不应该要求对象是列表,只是要求它们具有您需要的方法。collections模块提供了一组抽象基类,有点类似于Java接口。集合实例的任何类型。例如,序列将支持索引。
#3
6
Expression you are looking for may be:
你正在寻找的表达可能是:
...
return any( isinstance(e, list) for e in my_list )
Testing:
测试:
>>> my_list = [1,2]
>>> any( isinstance(e, list) for e in my_list )
False
>>> my_list = [1,2, [3,4,5]]
>>> any( isinstance(e, list) for e in my_list )
True
>>>
#1
98
Use isinstance
:
使用isinstance:
if isinstance(e, list):
If you want to check that an object is a list or a tuple, pass several classes to isinstance
:
如果要检查对象是list或tuple,请将几个类传递给isinstance:
if isinstance(e, (list, tuple)):
#2
20
-
Work out what specific properties of a
list
you want the items to have. Do they need to be indexable? Sliceable? Do they need an.append()
method?计算出你想要的条目的具体属性。它们需要可索引吗?切割的?他们需要一个.append()方法吗?
-
Look up the abstract base class which describes that particular type in the
collections
module.查找描述集合模块中特定类型的抽象基类。
-
Use
isinstance
:使用isinstance:
isinstance(x, collections.MutableSequence)
You might ask "why not just use type(x) == list
?" You shouldn't do that, because then you won't support things that look like lists. And part of the Python mentality is duck typing:
您可能会问“为什么不使用type(x) = list?”你不应该这样做,因为那样你就不会支持看起来像列表的东西。Python思维的一部分是鸭子类型:
I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck
我看见一只鸟像鸭子一样走路,像鸭子一样游泳,像鸭子一样嘎嘎叫,我把那只鸟叫做鸭子
In other words, you shouldn't require that the objects are list
s, just that they have the methods you will need. The collections
module provides a bunch of abstract base classes, which are a bit like Java interfaces. Any type that is an instance of collections.Sequence
, for example, will support indexing.
换句话说,您不应该要求对象是列表,只是要求它们具有您需要的方法。collections模块提供了一组抽象基类,有点类似于Java接口。集合实例的任何类型。例如,序列将支持索引。
#3
6
Expression you are looking for may be:
你正在寻找的表达可能是:
...
return any( isinstance(e, list) for e in my_list )
Testing:
测试:
>>> my_list = [1,2]
>>> any( isinstance(e, list) for e in my_list )
False
>>> my_list = [1,2, [3,4,5]]
>>> any( isinstance(e, list) for e in my_list )
True
>>>