Sunday, 15 January 2012

Python: How to create 2D array using existing lists -



Python: How to create 2D array using existing lists -

i have been trying resolve issue.

i have 3 lists

dev = ['alex', 'ashley', 'colin'] phone = ['iphone', 'nexus', 'nokia'] carrier = ['att', 't-mobile', 'megafon']

is pythonic way create new 2d array this:

matrix = [['alex', 'iphone', 'att'], ['ashley','nexus','t-mobile'], ['colin', 'nokia', 'megafon']]

thank you

i think want:

>>> zip(dev, phone, carrier) [('alex', 'iphone', 'att'), ('ashley', 'nexus', 't-mobile'), ('colin', 'nokia', 'megafon')]

see zip documentation.

if need list of lists (instead of list of tuples in python 2, or iterator in python 3), utilize padraic's suggestion: [list(x) x in zip(dev, phone, carrier)].

python arrays multidimensional-array

No comments:

Post a Comment