Python:初始化多维列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17636567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:45:37  来源:igfitidea点击:

Python: initialize multi-dimensional list

pythonlist

提问by fdmillion

I want to initialize a multidimensional list. Basically, I want a 10x10 grid - a list of 10 lists each containing 10 items.

我想初始化一个多维列表。基本上,我想要一个 10x10 的网格 - 一个包含 10 个列表的列表,每个列表包含 10 个项目。

Each list value should be initialized to the integer 0.

每个列表值都应初始化为整数 0。

The obvious way to do this in a one-liner: myList = [[0]*10]*10won't work because it produces a list of 10 references to one list, so changing an item in any row changes it in all rows.

在单行中执行此操作的显而易见的方法是:myList = [[0]*10]*10不起作用,因为它会生成对一个列表的 10 个引用的列表,因此更改任何行中的项目会更改所有行中的项目。

The documentation I've seen talks about using [:]to copy a list, but that still won't work when using the multiplier: myList = [0]*10; myList = myList[:]*10has the same effect as myList = [[0]*10]*10.

我看过的文档谈到使用[:]复制列表,但在使用乘数时仍然不起作用:myList = [0]*10; myList = myList[:]*10myList = [[0]*10]*10.

Short of creating a loop of myList.append()s, is there a quick efficient way to initialize a list in this way?

myList.append()除了创建s循环之外,是否有一种快速有效的方法来以这种方式初始化列表?

采纳答案by cheeyos

You can do it quite efficiently with a list comprehension:

您可以使用列表理解非常有效地做到这一点:

a = [[0] * number_cols for i in range(number_rows)]

回答by llb

This is a job for...the nested list comprehension!

这是...嵌套列表理解的工作

[[0 for i in range(10)] for j in range(10)]

回答by Ali Afshar

You might actually need an arrayinstead of some lists. Almost every time I see this "presized nested list" pattern, something is not quite right.

您实际上可能需要一个数组而不是一些列表。几乎每次我看到这种“预先设定的嵌套列表”模式时,都有些不对劲。

回答by jengel

Just thought I'd add an answer because the question asked for the general n-dimensional case and I don't think that was answered yet. You can do this recursively for any number of dimensions with the following example:

只是想我会添加一个答案,因为这个问题要求一般的 n 维情况,我认为还没有回答。您可以使用以下示例对任意数量的维度递归执行此操作:

n_dims = [3, 4, 5]

empty_list = 0
for n in n_dims:
    empty_list = [empty_list] * n

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

回答by Matt Eding

An additional solution is to use NumPy library:

另一个解决方案是使用 NumPy 库:

import numpy as np

zero_array = np.zeros((10, 10), dtype='int')

This can be easily converted to a regular python list with the .tolist()method if necessary.

.tolist()如有必要,可以使用该方法轻松地将其转换为常规的 Python 列表。

回答by 0xAliHn

Two common and short way to do this:

执行此操作的两种常见且简短的方法:

First:

第一的:

[[0] * n] * m

Second:

第二:

[[0 for column in range(n)] for row in range(m)]