Home > Software engineering >  Append np array with a determined value. Data isn't appending to list when using np.append
Append np array with a determined value. Data isn't appending to list when using np.append

Time:05-28

I am developing a code to use data from a light sensor to determine the gradient of an experiment. Part of the code requires appending 2 arrays (systemtime and volume total). The relevant part of my code reads:

systemtime = np.array([])
volumetotal = np.array([])
volume = 0
...
while True:
    a = time()
    ...
       np.append(systemtime , a)
     ...
       volume = volume   added
       np.append(volumetotal,volume)
       if added == 0:
           break       
ser.close()
print (systemtime)
time = systemtime-systemtime[0]

When printing system time the console shows and empty list [], therefore the final line of code doesn't work. How can I successfully append the lists? (... used to remove unnecessary bits of code)

CodePudding user response:

I suggest you assign the extension of your systemtime or volumetotal list to a variable.

systemtime = np.array([])
volumetotal = np.array([])
volume = 0
...
while True:
    a = time()
    ...
       systemtime = np.append(systemtime, a)
     ...
       volume = volume   added
       volumetotal = np.append(volumetotal, volume)
       if added == 0:
           break       
ser.close()
print (systemtime)
time = systemtime-systemtime[0]

Without this, your use of the append method from numpy has no effect: you only create locally (so on this single line) the list you want to create, without giving you the possibility to access it afterwards.

CodePudding user response:

Arrays do nothing for you here.

systemtime = []
volumetotal = []
volume = 0
...
while True:
    a = time()
    ...
       systemtime.append(a)
     ...
       volume = volume   added
       volumetotal.append(volume)
       if added == 0:
           break       
ser.close()
print (systemtime)
time = [t-systemtime[0] for t in systemtime]
  • Related