简体   繁体   English

从 while 循环创建一个列表

[英]Create a list from while loop

I have while loop.我有while循环。 And I want to create a list from while loop.我想从 while 循环中创建一个列表。 I mean I need to append each result of loop to the list.我的意思是我需要将循环的每个结果 append 到列表中。 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)启动一个列表: variableName=[] (在while循环之前)
  2. In the while loop add values, either with variableName.append(value) or variableName+=[value]在 while 循环中添加值,使用variableName.append(value)variableName+=[value]

Declared an lst[] outside of while loop.在 while 循环之外声明了一个lst[] Then append a to list.然后 append a列。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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