1

Is any function that let me to add any number (e.g. "0") at end of every row in array. Example: I have two dimensional array:

ar=[[0,0,1],
[1,1,1],
[0,1,0]]

And I want add it to other 1-dimensional array, so I have:

 otherarray=numpy.array([],dtype=bool)
    otherarray=np.append(otherarray, ar)

Result:

 otherarray=[0,0,1,1,1,1,0,1,0]

And it works. But I need to add to every row of ar any number, e.g. 0 and get it as a result in otherarray (not modifying ar). Result I want:

[0,0,1,0,1,1,1,0,0,1,0,0]

I am doing it using for loop (I'm putting every element to otherarray one-by-one) but now I'm asking: is any better way?

1 Answer 1

1

You can append a zero column to ar and then flatten it:

A = np.array(ar)    

np.hstack([A, np.zeros((A.shape[0], 1), dtype=A.dtype)]).ravel()
# array([0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0])
Sign up to request clarification or add additional context in comments.

1 Comment

Great idea! Thanks!

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.