Home > Blockchain >  numpy "insert" does not insert anything
numpy "insert" does not insert anything

Time:04-30

hello good people I have issues with the Numpy python library in inserting date to the array here the simple 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 start python recently so I'm beginner but I have the basics

if you see any English errors ,this is because I am not a native English speaker

and think you :]

CodePudding user response:

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 =
  • Related