Combining elements of a numpy array using join () in python

Want to convert the following numpy a array

a = [ array([['x', 'y', 'k'], ['d', '2', 'z'], ['a', '15', 'r']], dtype='|S2'), array([['p', '49'], ['l', 'n']], dtype='|S2'), array([['5', 'y'], ['33', 'u'], ['v', 'z']], dtype='|S2'), array([['f', 'i'], ['c', 'm'], ['u', '98']] dtype='|S2') ] 

to exit b

  b = x!yd!2.a!15 * x!kd!za!r , p!49.l!n , 5!y.33!uv!z , f!ic!mu!98 

Consider each subobject similar to this.

  #x #y #k #d #2 #z #a #15 #r 

Then combine the 0 and 1 columns with '!' and each line with '.' Then merge 0 and 2 columns. And so on. Here, columns 0,1 and 0,2 are combined using '*' AND ',' is used to combine sub-arrays

Just merging strings with '!' '.' '*' ',' '!' '.' '*' ','

I tried the following code. Failed to get result though

 temp = [] for c in a: temp = a[0:] b = " * ".join(".".join(var1+"!"+var2 for var1,var2 in zip(temp,a) for row in a) print b print " , " 
+6
source share
2 answers
 In [144]: " , ".join(" * ".join(".".join(str(xx) + '!' + str(yy) for xx, yy in zip(x[:, 0], x[:, _x])) for _x in range(1, len(x[0]))) for x in a) Out[144]: 'x!yd!2.a!15 * x!kd!za!r , p!49.l!n , 5!y.33!uv!z , f!ic!mu!98' 
+4
source

As I understand it, your solution might be this:

 import numpy as np a = [ np.array([['x', 'y', 'k'], ['d', '2', 'z'], ['a', '15', 'r']], dtype='|S2'), np.array([['p', '49'], ['l', 'n']], dtype='|S2'), np.array([['5', 'y'], ['33', 'u'], ['v', 'z']], dtype='|S2'), np.array([['f', 'i'], ['c', 'm'], ['u', '98']], dtype='|S2') ] aa = [] for k in a: bb = [] for i in range(len(k[0]) - 1): cc = [] for j in range(len(k)): cc.append('!'.join([k[j][0], k[j][i + 1]])) bb.append('.'.join(cc)) aa.append(' * '.join(bb)) b = ' , '.join(aa) print b 

If this is not correct, please clarify your algorithm.

+4
source

All Articles