Python3中将列表初始化为同一值[1 for _ in range(10)]

1

ls = [0 for _ in range(10)]
print(ls)

输出为,

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

2

ls = [False for _ in range(10)]
print(ls)

输出为,

[False, False, False, False, False, False, False, False, False, False]

3

ls = [[0, 0, 0] for _ in range(10)]
print(ls)

输出为,

[[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]]
上一篇:发布一个php分页类


下一篇:【无标题】