python中列表全部赋0

python中能不能像C那样,比如我新建一个列表,并赋100个初值为0
2024-12-18 17:50:18
推荐回答(3个)
回答1:

>>> list=[0]
>>> list=list*100
>>> list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> len(list)
100

回答2:

标准写法:
listVal = [];
for i in range(100):
listVal.append(0);

快速写法1:
listVal = [[0]*100];

快速写法2:
listZero = [0]
listVal = listZero * 100;

回答3:

PythonList = [0] * 100就可以啦