6

Want to convert the following numpy array a

    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')
    ]

into output b

  b =  x!y.d!2.a!15 * x!k.d!z.a!r , p!49.l!n , 5!y.33!u.v!z , f!i.c!m.u!98

Consider each sub-array like this

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

Then merge 0 and 1 columns with '!' and each row with '.' Then merge 0 and 2 column. And so on. Here 0,1 and 0,2 columns are merged using '*' And ',' is used to merge the sub-arrays

Just merging the strings using '!' '.' '*' ','

I tried the following code. Couldnt get the 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 " , "

2 Answers 2

4
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!y.d!2.a!15 * x!k.d!z.a!r , p!49.l!n , 5!y.33!u.v!z , f!i.c!m.u!98'
Sign up to request clarification or add additional context in comments.

Comments

4

How I understand, your solution maybe like 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 it's wrong, please, clarify your algorithm.

3 Comments

@Chloe, please, give more examples, to understand better
I modified it @Abylay
I think i am a bit confused of your indentations @Abylay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.