从while循环创建列表

2024-04-20 11:44:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

Tags: 代码loop列表lenfloatlengthfilteredlist
2条回答

在while循环外部声明了一个lst[]。然后将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)
  1. 启动一个列表:variableName=[](在while循环之前)
  2. 在while循环中,使用variableName.append(value)variableName+=[value]添加值

相关问题 更多 >