0

I need help for my project. I have an array that look like this?

rndm = [[0 1]
        [0 0]
        [0 0]
        [0 1]]

Now, I want to add par_1 = [[1 0]], par_2 = [[0 0], ch1 = [[1 1]], and ch2 = [[0 1]] to rndm.

My code looks like this:

new_rndm = []
new_rndm.append(par_1)
new_rndm.append(par_2)
new_rndm.append(ch1)
new_rndm.append(ch2)
# add them to rndm
rndm = numpy.append(rndm, [new_rndm])
print(rndm)

The output gives me something like this:

rndm = [0 1 0 0 0 0 0 1 1 0 0 0 1 1 0 1]

What I am expecting as my out put is:

rndm = [[0 1]
        [0 0]
        [0 0]
        [0 1]
        [1 0]
        [0 0]
        [1 1]
        [0 1]]

I think the problem is that append cannot be used in arrays. If correct, anyone help me what other function I could try? If not, kindly educate me. I am very much willing to learn. Thank you!

4
  • Is it a numpy array ? Commented Jun 4, 2020 at 13:15
  • @Sushanth rndm is an array. Commented Jun 4, 2020 at 13:17
  • we are able to see it is an array but is it python nested list or a numpy array ? Commented Jun 4, 2020 at 13:18
  • @Sushanth my bad, it is a numpy array Commented Jun 4, 2020 at 13:19

3 Answers 3

3

Use np.append(<array>, <elem to append>, axis=0)

rndm = np.array([[0, 1],
        [0, 0],
        [0, 0],
        [0, 1]])

par_1 = [[1, 0]]; par_2 = [[0, 0]]; ch1 = [[1, 1]]; ch2 = [[0, 1]]

rndm = np.append(rndm, par_1, axis=0)
rndm = np.append(rndm, par_2, axis=0)
rndm = np.append(rndm, ch1, axis=0)
rndm = np.append(rndm, ch2, axis=0)

array([[0, 1],
       [0, 0],
       [0, 0],
       [0, 1],
       [1, 0],
       [0, 0],
       [1, 1],
       [0, 1]])

Edit:

Reshape:

x = np.array([2,1])
y = x.reshape(-1,1) # <------------ you have to do this
x.shape, y.shape

((2,), (2, 1))
Sign up to request clarification or add additional context in comments.

13 Comments

Hello! Thank you so much for your answer! But why is it that I am getting an error trying this solution? Error is: ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
What numpy version do you use? I use 1.18.4
Are you using the whole code or some part of it. Because I am not getting any error.
I am using the same version of numpy. This is just a part of my code. rndm is generated randomly as well as par_1 and the others.
This (2, ) is the cause of this issue. reshape it to (2,1)
|
0

You can use .append to add an array to the end of another array. The problem here is that numpy.append flattens the array first, ie. numpy.append([1 0], [0 1]) is [1 0 0 1]. See the numpy docs on .append.

2 Comments

It is because rndm is a numpy array. So would it be better to make it as list then append those values?
The reason why it is appending like that because shape of those two are (2,) They are flattened that's why you will get that answer only but if you have shape like this (2,1) then you can append row wise.
0

You can use ordinary list appending to generate the desired nested list structure:

rndm = [[0, 1],
        [0, 0],
        [0, 0],
        [0, 1]
        ]

par_1 = [[1, 0]]
par_2 = [[0, 0]]
ch1 = [[1, 1]]
ch2 = [[0, 1]]

new_rndm = []

new_rndm.append(par_1)
new_rndm.append(par_2)
new_rndm.append(ch1)
new_rndm.append(ch2)

new_rndm = [i for k in new_rndm for i in k]

for data in new_rndm:
    rndm.append(data)

for data in rndm:
    print(data)

Outputs:

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

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.