[英]Python: How to create 2D array using existing lists
I have been trying to resolve this issue. 我一直在尝试解决此问题。
I have 3 lists 我有3个清单
dev = ['Alex', 'Ashley', 'Colin']
phone = ['iPhone', 'Nexus', 'Nokia']
carrier = ['ATT', 'T-Mobile', 'MegaFon']
Is this a pythonic way to create a new 2D array like this: 这是创建这种新2D数组的pythonic方法吗?
matrix = [['Alex', 'iPhone', 'ATT'], ['Ashley','Nexus','T-Mobile'], ['Colin', 'Nokia', 'MegaFon']]
Thank you 谢谢
I think you want: 我想你要:
>>> zip(dev, phone, carrier)
[('Alex', 'iPhone', 'ATT'),
('Ashley', 'Nexus', 'T-Mobile'),
('Colin', 'Nokia', 'MegaFon')]
See zip
documentation . 请参阅
zip
文档 。
If you need a list of lists (instead of a list of tuples in Python 2, or an iterator in Python 3), use Padraic's suggestion: [list(x) for x in zip(dev, phone, carrier)]
. 如果您需要一个列表列表(而不是Python 2中的元组列表或Python 3中的迭代器),请使用Padraic的建议:
[list(x) for x in zip(dev, phone, carrier)]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.