创建一个循环来将列表中的int更改为另一个int?

时间:2015-10-10 02:54:49

标签: python

如何获取数字列表

numList = []

while len(numList) <= 1000:
    numList.append(1)

while len(numList) <= 1000:
    numList.append(0)

print(numList)

现在我该如何将这些更改为0?

for for循环?

3 个答案:

答案 0 :(得分:2)

您没有更改添加的第一组元素。请参阅我添加到您的代码中的评论:

numList = []

while len(numList) <= 1000:
    numList.append(1)

# the numList is already a thousand elements long, so the while loop
# body is NEVER executed.
while len(numList) <= 1000:
    numList.append(0)

请改为尝试:

numList = []

while len(numList) <= 1000:
    numList.append(1)

for i in range(0, 1000):
    numList[i] = 0

print(numList)

答案 1 :(得分:0)

更改您要附加的内容:

while len(numList) <= 1000:
  numList.append(0)

除非您将现有的[1,1,1,...]数组更改为[0,0,0,...],否则您可以使用列表推导:

numList = [0 for x in range(len(numList))]

答案 2 :(得分:0)

另一种方法,更清楚;

>>> 
[0, 0, 0, 0, 0]
>>> 

结果;

Error while pulling image: Get https://index.docker.io/v1/repositories/library/redis/images: dial tcp: lookup index.docker.io on 66.170.14.12:53: server misbehaving