简体   繁体   中英

How to append values to array via for-loop

I have three sets np.arrays. Two is my dataset (U and t) and the third is the number of each data point (iarray).I am attempting to make three new arrays: one contains the values of (U) within a certain interval. The second contains the values of (t) attributed to the values of (U). The third contains the number of those values in the original array (they should be the same for both U and t).

I get two arrays of different size for the new U and t and my array of integers contains only one value.

All should have the same size.

I have attempted to setup a series of for-loops that checks the value of U to be within a certain interval. Then check if the value in t is to close to the last point added to the new t-array. If all is well it should append the values with the same integers in the original arrays at the same positions in the new arrays.

Arrays with data and array of numbers

t = a[:,0]
U = a[:,1]
iarray=np.array(range(len(t)))

Core of the code

tpeak = np. array([])
Upeak = np. array([])
b=np.array([])

for i in range(len(t)):
    if (np.size(b)==0) and 0.9<U[i]and U[i]<4 :
        tpeak=np.append(tpeak,t[i])

for i in range(len(t)):
    if (np.size(b)==0) and 0.9<U[i]and U[i]<4 :
       Upeak=np.append(Upeak,U[i])

for i in range(len(t)):
    if (np.size(b)==0) and 0.9<U[i]and U[i]<4 :
       b=np.append(b,iarray[i])

for i in range(len(t)):
    if U[i]>1 and U[i]<4  and 0.8<tpeak[-1]-t[i] : 
        tpeak=np.append(tpeak,t[i])

for i in range(len(t)):
    if U[i]>1 and U[i]<4  and 0.8<tpeak[-1]-t[i] :
        Upeak=np.append(Upeak,U[i])


for i in range(len(t)):
    if U[i]>1 and U[i]<4  and 0.8<tpeak[-1]-t[i] : 
        b=np.append(b,iarray[i])

As mentioned I expect the output to be three arrays (Upeak,tpeak and b) of the same size yet U peak is 1 smaller than tpeak and ipeak contains only one value.

I don't understand what's your question, by the way, you can use list comprenshion so that your code will be much more cleaner. Instead of:

tpeak = np. array([])
Upeak = np. array([])
b=np.array([])

for i in range(len(t)):
    if (np.size(b)==0) and 0.9<U[i]and U[i]<4 :
        tpeak=np.append(tpeak,t[i])

for i in range(len(t)):
    if (np.size(b)==0) and 0.9<U[i]and U[i]<4 :
       Upeak=np.append(Upeak,U[i])

for i in range(len(t)):
    if (np.size(b)==0) and 0.9<U[i]and U[i]<4 :
        b=np.append(b,iarray[i])

 for i in range(len(t)):
    if U[i]>1 and U[i]<4  and 0.8<tpeak[-1]-t[i] : 
        tpeak=np.append(tpeak,t[i])

for i in range(len(t)):
    if U[i]>1 and U[i]<4  and 0.8<tpeak[-1]-t[i] :
        Upeak=np.append(Upeak,U[i])


for i in range(len(t)):
    if U[i]>1 and U[i]<4  and 0.8<tpeak[-1]-t[i] : 
        b=np.append(b,iarray[i])

You can use:

tpeak=[t[i] if (np.size(b)==0) and 0.9<U[i]and U[i]<4 for i in range(len(t)):]

or maybe try to optimize the for loop.

for i in range(len(t)):
    ...

Because as you see, you repeate that piece of code over 6 times. If you show us the output of this code would be great so that we then could help you better.

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