在一个循环中将值存储在数组中的问题

时间:2019-04-06 12:10:39

标签: python arrays loops

我想在我的778个数组中存储值0.2。但结果显示“无”作为值。

我想做什么: 778的[0.2,0.2,0.2]

[无,  没有,  没有,  没有,  没有,  没有,  没有,  没有,  没有,  没有,  没有,  没有。...]

weight=[]
i=0
while i <=777:
    value=0.2
    weight[i]= weight.append(value)
    i=i+1
weight

预期结果: [0.2,0.2,0.2,0.2] ~~~~直到778次迭代

3 个答案:

答案 0 :(得分:3)

只需weight.append(value)

使用weight[i]= weight.append(value),将weight[i]设置为append的返回值。 append修改该列表,并且没有任何有用的值可返回,因此它返回None

答案 1 :(得分:1)

错误在这里 weight[i]= weight.append(value) 。应该是weight.append(value)

答案 2 :(得分:1)

您可以使用这种方法解决它。

 >>> [.2 for i in range(10)]
 [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]