简体   繁体   中英

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

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)

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM