简体   繁体   中英

Create a list from while loop

I have while loop. And I want to create a list from while loop. I mean I need to append each result of loop to the list. I am sharing my code belog.

length = len(filtered)
i = 0
  
# Iterating using while loop
while i < length-1:
    a = filtered[i+1][0], (float(filtered[i+1][1])-float(filtered[i][1]))/(float(filtered[i][1]))
    
    listt=list(a)
    
    i += 1
    
    print(listt)
  1. Initiate a list: variableName=[] (before the while loop)
  2. In the while loop add values, either with variableName.append(value) or variableName+=[value]

Declared an lst[] outside of while loop. Then append a to list.

length = len(filtered)
i = 0

# Iterating using while loop
lst = []
while i < length-1:
    a = (filtered[i+1][0], (float(filtered[i+1][1]) -float(filtered[i][1]))/(float(filtered[i][1])))
    lst.append(a)
    i += 1
print(lst)

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