简体   繁体   English

创建两个零列表的列表

[英]Create list of two lists of zeros

I'm trying to create a list of two lists of zeros for 20 times, like this:我正在尝试创建一个包含两个零列表的列表 20 次,如下所示:

[[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]]

I tried this:我试过这个:

output = []
for j in range(20) :
    output.append([0])

I which it gives me:它给了我的我:

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

But this is not what I want:(但这不是我想要的:(

Any help, please?请问有什么帮助吗?

Having a list like [[0][0], [0][0]] is not a valid syntax in Python, but you can have something like this: [[0, 0]] by doing the following.在 Python 中,拥有类似[[0][0], [0][0]]的列表不是有效的语法,但您可以通过执行以下操作获得类似这样的内容: [[0, 0]]

zeros_list = [[0, 0] for _ in range(20)]

output output

[[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]]

Or you can have something like this [[[0], [0]], [[0], [0]]] , which would be a list of lists of lists containing zeros by doing:或者你可以有这样的东西[[[0], [0]], [[0], [0]]] ,这将是一个包含零的列表列表的列表:

zeros_list = [[[0], [0]] for _ in range(20)]

Another way:另一种方式:

result = [[0, 0]] * 20
print (result)
#[[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]]

result = [[[0], [0]]] * 20
print (result)
#[[[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]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM