繁体   English   中英

从 while 循环创建一个列表

[英]Create a list from while loop

我有while循环。 我想从 while 循环中创建一个列表。 我的意思是我需要将循环的每个结果 append 到列表中。 我正在分享我的代码。

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. 启动一个列表: variableName=[] (在while循环之前)
  2. 在 while 循环中添加值,使用variableName.append(value)variableName+=[value]

在 while 循环之外声明了一个lst[] 然后 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