Reputation: 33
I'm trying to simply stack two 2D arrays onto each other like this, if I have two 2D arrays like this:
a = [[0, 0],
[0, 0]]
b = [[1, 1],
[1, 1]]
I want this output:
ab = [['01', '01'],
['01', '01']]
It is important that the final array elements (e.g. 01) are one string and not seperate elements(like ['0','1']). I've been trying with zip and dstack but cannot get it right.
Edit: I didn't mention this in the OP but the output matrix should also have the same dimensions NxN dimensions as the 2 input matrices.
Upvotes: 0
Views: 131
Reputation: 865
That's actually quite an interesting question if array dimensions are quite large.
You can try dstack
+apply_along_axis
approach. Here, dstack
would sandwich two your arrays atop each other, and apply_along_axis(.. , 2, ..)
would apply a conversion function to each 1x1x2 canapé of that sandwich.
The trick is to provide conversion function, here I propose "convert to int, convert to string and join strings" as a simple nested lambda.
import numpy as np
np.apply_along_axis(lambda x: ''.join(map(lambda y:str(int(y)),x)), 2, np.dstack((np.array(a),np.array(b))))
>>> a = np.zeros((10,10))
>>> a
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> b = np.ones((10,10))
>>> b
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
>>> np.apply_along_axis(lambda x: ''.join(map(lambda y:str(int(y)),x)), 2, np.dstack((a,b)))
array([['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01'],
['01', '01', '01', '01', '01', '01', '01', '01', '01', '01']],
dtype='<U2')
Upvotes: 1
Reputation:
You can do this in a double comprehension list
Edit: Added an int cast to make sure the values don't get interpreted as floats
a = [[0, 0], [0, 0]]
b = [[1, 1], [1, 1]]
result = [[str(int(i)) + str(int(j))] for c,d in zip(a,b) for i,j in zip(c,d)]
result = [[result[0][0], result[1][0]], [result[2][0], result[3][0]]]
print result
[['01', '01'], ['01', '01']]
Upvotes: 1
Reputation: 1720
If both 2D arrays always have same size m*n
, you can try using this basic nested-looping:
a = [[0, 0],
[0, 0]]
b = [[1, 1],
[1, 1]]
c = []
for i in range(len(a)):
temp = []
for j in range(len(a[i])):
temp.append(str(a[i][j])+str(b[i][j]))
c.append(temp)
Upvotes: 0
Reputation: 817
you can try the following if you want the result as strings, otherwise remove the str method
c = [[str(x[0]) + str(y[0]), str(x[1]) + str(y[1])] for x, y in list(zip(a, b))]
Upvotes: 0