1

I have an numpy ndarray input like this

[[T, T, T, F],
  [F, F, T, F]]

and I want to duplicate every value as a new array, so the output would be

[[[T,T], [T,T], [T,T], [F,F]]
  [[F,F], [F,F], [T,T], [F,F]]]

How can I do this? Thank you in advance

yatu
  • 86,083
  • 12
  • 84
  • 139
ErgiS
  • 223
  • 2
  • 10

2 Answers2

4

One way would be using np.dstack to replicate the array along the third axis:

np.dstack([a, a])

array([[['T', 'T'],
        ['T', 'T'],
        ['T', 'T'],
        ['F', 'F']],

       [['F', 'F'],
        ['F', 'F'],
        ['T', 'T'],
        ['F', 'F']]], dtype='<U1')

Setup:

T = 'T'
F = 'F'
a = np.array([[T, T, T, F],
              [F, F, T, F] ])
yatu
  • 86,083
  • 12
  • 84
  • 139
0

you can just use list comprehension:

data =[['T', 'T', 'T', 'F'],
  ['F', 'F', 'T', 'F'] ]

d = [[[i]*2 for i in j] for j in data]
print (d)

output:

[[['T', 'T'], ['T', 'T'], ['T', 'T'], ['F', 'F']], [['F', 'F'], ['F', 'F'], ['T', 'T'], ['F', 'F']]]
ncica
  • 7,015
  • 1
  • 15
  • 37