且构网

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

遍历Python中的列表

更新时间:2023-11-02 23:44:40

尝试一下,

x in mylistx in mylist[:]更好并且更具可读性,并且len(x)应该等于3.

x in mylist is better and more readable than x in mylist[:] and your len(x) should be equal to 3.

>>> mylist = [[1,2,3],[4,5,6,7],[8,9,10]]
>>> for x in mylist:
...      if len(x)==3:
...        print x
...
[1, 2, 3]
[8, 9, 10]

或者如果您需要更多的Python语言,请使用列表理解

or if you need more pythonic use list-comprehensions

>>> [x for x in mylist if len(x)==3]
[[1, 2, 3], [8, 9, 10]]
>>>