I have issues with the Numpy python library in inserting date to the array. Here is the code that I have a problem with:
import numpy as np
arr = np.array([]) # here is the array
UserInput = int(input("type the lenght of the array")) # the user decide the lenght of the array
arr.resize(UserInput)
#now if we print the array it will be [0,0,0,0,0,0...]
def inserting(): # simple function that let the user choose which index he/she want to change
...
TheUserChoose = int(input("choose the index that you want to change its value")) # now let's assume the user choose the index number '1'
theNewValue = int(input("type here")) # we type '7' okay
np.insert(arr,TheUserChoose ,theNewValue )
print(arr)
>>> [0,0,0,0,...0] Nothing change
# Also if I type the index and the value manually like "np.insert(arr,1 ,7)" nothing happened
I started python only recently, so I’m a beginner, but I have the basics.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
NumPy’s arrays are immutable (for the most case), so np.insert does not do it in-place, instead returns a new array so you’d need:
arr = np.insert(arr,TheUserChoose ,theNewValue ) #note the arr =
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0