Combining a 1D arrays of the same length into a 2D array

6 views
Skip to first unread message

Jason Hsu

unread,
Jan 28, 2012, 12:55:30 AM1/28/12
to PyMNtos
array1 consists of:
[
stringa0
stringa1
stringa2
...
stringan
]

array2 consists of:
[
stringb0
stringb1
stringb2
...
stringbn
]

I'm looking for a way to create array3:
[
stringa0 stringb0
stringa1 stringb1
stringa2 stringb2
...
stringan stringbn
]

How do I do this?

Zachary Crockett

unread,
Jan 28, 2012, 1:33:22 AM1/28/12
to pym...@googlegroups.com
One way is to use a list comprehension with enumerate:

inlist1 = ['a0', 'a1', 'a2', 'a3']
inlist2 = ['b0', 'b1', 'b2', 'b3']
outlist = [[item, inlist2[index]] for index, item in enumerate(inlist1)]

Dan Callahan

unread,
Jan 28, 2012, 1:37:08 AM1/28/12
to pym...@googlegroups.com, Jason Hsu
You're looking for the built-in `zip` function.

Aaron Cannon

unread,
Jan 28, 2012, 7:42:28 PM1/28/12
to pym...@googlegroups.com
Just to elaborate, because reading the docs is hard, :) zip(array1,
array2) will do exactly what you want.

Aaron

Mark Holmquist

unread,
Jan 28, 2012, 11:39:38 AM1/28/12
to pym...@googlegroups.com
> One way is to use a list comprehension with enumerate:

> You're looking for the built-in `zip` function.

I like both answers! But in case you want to do other computations on
the list during the zip:

array3 = [s for s in zip(array1, array2)]

However, careful reading of the python docs leads me to believe that you
will wind up with a list of tuples, so maybe you need something
else....maybe it's not possible with list comprehension, because you'd
need to split the zipped array afterwards. Of course, the first
suggestion works best, but it doesn't strike me as particularly pythonic
(indexes in list comprehension? what is this, javascript?).

OK, got it. I don't think it's possible without using a for loop, so it
will be slower than the one with indexes, but just as an exercise in
pythonicity:

array3 = []
for a in zip(array1, array2):
array3.extend(a)
# here, array3 should contain [array1[0], array2[0], array1[1], ....
array2[n]].

Sorry I chatted you up and wound up with a slow solution :) then again,
the python documentation on list comprehensions uses an index, too, so I
guess you'd be OK with it!

--
Mark Holmquist
Student, Computer Science
University of Redlands
MarkT...@gmail.com

jay

unread,
Feb 7, 2012, 4:48:12 PM2/7/12
to PyMNtos
> OK, got it. I don't think it's possible without using a for loop, so it
> will be slower than the one with indexes, but just as an exercise in
> pythonicity:
>
> array3 = []
> for a in zip(array1, array2):
>         array3.extend(a)
> # here, array3 should contain [array1[0], array2[0], array1[1], ....
> array2[n]].

And for the loop averse:

array1 = range(10)
array2 = range(2, 12)
array3 = []
map(array3.extend, zip(array1, array2))

now, array3 has the flattened zippping of array1 and array2. I'm
still on the fence about whether any sort of "real" functional
programming is pythonic, but I personally love the map function.
Reply all
Reply to author
Forward
0 new messages