0

My goal is for nd_array[0] = 1,2,3,4,5 I have sat on this problem for hours now. I have attempted to create the whole 2D list as a list and then using np.asarray(just creates the strangiest array i havbe ever seen, not even sure how to explain it) , I've tried np.append this is just a cut out from a different problem. In the actual example, I am looping and creating a list with 5 values, then adding those values to nparray[0] then the loop after adding to nparray[1]


nd_array = np.zeros(shape=(5, 5))

pyth_list = [1, 2, 3, 4, 5]

np.insert(nd_array, pyth_list, 0)


print(123)
3
  • 1
    nd_array[0] = pyth_list Commented Mar 24, 2020 at 14:56
  • np.insert(nd_array, pyth_list, 0) this obviously doesn't do anything since np.insert doesn't mutate inplace, but instead returns a new array. I also don't know what this print(123) has to do with anything? Commented Mar 24, 2020 at 14:56
  • The way I normally figure out how something works in programming, especially in small extracted programs like these, is to use the debugger and inspect how elements change. the final line means that it wont finish the debugging once i click F8 after the np.insert() line and also lets me put a breakpoint there to inspect faster without having to click F8 @ruohola Commented Mar 24, 2020 at 18:00

3 Answers 3

1

you can use:

nd_array[0] = [1, 2, 3, 4, 5]
nd_array

output:

array([[1., 2., 3., 4., 5.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
Sign up to request clarification or add additional context in comments.

1 Comment

it's just that easy. 1-2hours well spent
0

You can use vstack()

nd_array = np.zeros(shape=(5, 5))

pyth_list = [1, 2, 3, 4, 5]

np.vstack([pyth_list,nd_array])

Comments

0

Yes you can, it's as simple as:

import numpy as np

nd_array = np.zeros(shape=(5, 5))
pyth_list = [1, 2, 3, 4, 5]

nd_array[0] = pyth_list
print(nd_array)

Output:

[[1. 2. 3. 4. 5.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]

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.