2

I have this problem where I would prefer not using loops because I am working with a big data as a solution :

This is what I am trying to do : (I know this works to [6 6 6], but I want to "join" it by index)

import numpy as np

np_1 = np.asarray([1,1,1])
np_2 = np.asarray([2,2,2])
np_3 = np.asarray([3,3,3])

np_4 = np_1 + np_2 + np_3
# np_4 should be [1,2,3,1,2,3,1,2,3]

Are there ways to do this? or should I look for options outside of numpy?

2

3 Answers 3

0

Try this:

np.array([np_1, np_2, np_3]).transpose().flatten()
Sign up to request clarification or add additional context in comments.

Comments

0

You can try the following method:

np.ravel(np.stack(np_1,np_2,np3),'F')

1 Comment

I think you are missing a set of brackets. This is a TypeError as currently posted. Also the typo: np3 -> np_3.
0

One way to do it is to stack the sequences depth-wise and flatten it:

np.dstack([np_1, np_2, np_3]).flatten()

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.