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

2
  • tried at least some code? Commented Jul 9, 2019 at 10:23
  • 1
    Yeah but I was wondering if there was a one line method without using a loop. Commented Jul 9, 2019 at 10:25

2 Answers 2

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] ])
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.