ErgiS
ErgiS

Reputation: 223

Duplicate every value in array as a new array

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

Upvotes: 1

Views: 59

Answers (2)

yatu
yatu

Reputation: 88226

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

Upvotes: 4

ncica
ncica

Reputation: 7206

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

Upvotes: 0

Related Questions