简体   繁体   English

遍历嵌套列表

[英]Iterating through nested list

How do i create a function to iterate my list in this manner. 我如何创建以这种方式迭代列表的函数。 Seems simply but im stuck... 看起来很简单,但即时通讯卡住了...

    myList= [[1,2,3], [4,5,6], [7,8,9]]

    def name(myList):
        somework..

    newList = [[1,4,7]. [ 2,5,8], [3,6,9]]
In [3]: zip(*myList)
Out[3]: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

if you specifically want list 如果您特别想列出

In [4]: [list(x) for x in zip(*myList)]
Out[4]: [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

for more details on zip function look at this 有关zip功能的更多详细信息,请查看

zip is what you want + argument unpacking. zip是您想要的+参数解zip It's awesome. 这很棒。 I like to think of it as python's builtin transpose. 我喜欢将其视为python的内置转置。

newList = zip(*myList)

This will actually give you an iterable (python3.x) or list (python2.x) of tuple , but that's good enough for most purposes. 实际上,这将为您提供一个可迭代的(python3.x)或list (python2.x)的tuple ,但这对于大多数用途而言已经足够了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM