如何申请功能拉链到n-列表 [英] How to apply function zip to n-list

查看:120
本文介绍了如何申请功能拉链到n-列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个例子应用功能拉链两个ALIST是这样的:

X = [1,2,3]
Y = [4,5,6]
压缩ZIP =(X,Y)
#显示
列表(压缩)
[(1,4),(2,5),(3,6)]

但现在,如果我有的像:

阵列= [[1,2,3],[3,4,5],[6,7,8 ] ...]

如何应用功能拉链表现出一些这样的:

[(1,3,6,...),(2,4,7,...) (3,5,8,...),...(...)]


解决方案

您需要说法通过图示或明星运营商拆包

 拉链(*数组)

例如:

 >>>数组= [[1,2,3],[3,4,5],[6,7,8]
>>>打印(列表(ZIP(*数组)))
[(1,3,6),(2,4,7),(3,5,8)]

The example applied the function zip to two alist is this:

x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
#show
list(zipped)
[(1, 4), (2, 5), (3, 6)]

But now if I've some like :

array = [   [1,2,3], [3,4,5] , [6,7,8] ... ]

How to applied the function zip to show some like:

[(1,3,6,...),(2,4,7,...),(3,5,8,...),... (....) ]

解决方案

You need argument unpacking via the "splat" or "star" operator:

zip(*array)

example:

>>> array = [   [1,2,3], [3,4,5] , [6,7,8]  ]
>>> print ( list(zip(*array)) )
[(1, 3, 6), (2, 4, 7), (3, 5, 8)]

这篇关于如何申请功能拉链到n-列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆