我的实际示例涉及更多,因此我将概念归结为一个简单的示例:
l = [1,2,3,4,5,6,7,8]
for number in l:
calc = number*10
print calc
对于循环的每次迭代,我都会得到一个变量 ( calc
),我想用它来填充一个新列表。我的实际过程不仅仅是将值乘以10
,因此我希望能够通过这种方法设置新列表中的每个值。新代码可能如下所示:
l = [1,2,3,4,5,6,7,8]
for number in l:
calc = number*10
# set calc as x'th entry in a new list called l2 (x = iteration cycle)
print l2
然后它将打印新列表:[10,20,30,40,...]