python 如何在python中创建嵌套列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2173087/
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-11-03 23:58:46  来源:igfitidea点击:

How to create nested lists in python?

pythonlistarraysmatrixnested-lists

提问by Jader Dias

I know you can create easily nested lists in python like this:

我知道您可以像这样在 python 中轻松创建嵌套列表:

[[1,2],[3,4]]

But how to create a 3x3x3 matrix of zeroes?

但是如何创建一个 3x3x3 的零矩阵呢?

[[[0] * 3 for i in range(0, 3)] for j in range (0,3)]

or

或者

[[[0]*3]*3]*3

Doesn't seem right. There is no way to create it just passing a list of dimensions to a method? Ex:

好像不太对 没有办法只将维度列表传递给方法来创建它吗?前任:

CreateArray([3,3,3])

回答by

In case a matrix is actually what you are looking for, consider the numpy package.

如果矩阵确实是您要查找的内容,请考虑使用 numpy 包。

http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros

http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros

This will give you a 3x3x3 array of zeros:

这会给你一个 3x3x3 的零数组:

numpy.zeros((3,3,3)) 

You also benefit from the convenience features of a module built for scientific computing.

您还可以从为科学计算构建的模块的便利功能中受益。

回答by rob

List comprehensions are just syntactic sugar for adding expressiveness to list initialization; in your case, I would not use them at all, and go for a simple nested loop.

列表推导式只是为列表初始化增加表现力的语法糖;在您的情况下,我根本不会使用它们,而是使用简单的嵌套循环。

On a completely different level: do you think the n-dimensional arrayof NumPy could be a better approach?
Although you can use lists to implement multi-dimensional matrices, I think they are not the best tool for that goal.

在完全不同的层面上:您认为NumPy的n 维数组可能是更好的方法吗?
尽管您可以使用列表来实现多维矩阵,但我认为它们不是实现该目标的最佳工具。

回答by Jader Dias

NumPy addresses this problem

NumPy 解决了这个问题

http://www.scipy.org/Tentative_NumPy_Tutorial#head-d3f8e5fe9b903f3c3b2a5c0dfceb60d71602cf93

http://www.scipy.org/Tentative_NumPy_Tutorial#head-d3f8e5fe9b903f3c3b2a5c0dfceb60d71602cf93

>>> a = array( [2,3,4] )
>>> a
array([2, 3, 4])
>>> type(a)
<type 'numpy.ndarray'>

But if you want to use the Python native lists as a matrix the following helper methods can become handy:

但是如果你想使用 Python 原生列表作为矩阵,下面的辅助方法会变得很方便:

import copy

def Create(dimensions, item):
    for dimension in dimensions:
        item = map(copy.copy, [item] * dimension)
    return item
def Get(matrix, position):
    for index in position:
        matrix = matrix[index]
    return matrix
def Set(matrix, position, value):
    for index in position[:-1]:
        matrix = matrix[index]
    matrix[position[-1]] = value

回答by Sander Evers

Or use the nest function defined here, combined with repeat(0) from the itertools module:

或者使用这里定义的嵌套函数,结合来自 itertools 模块的 repeat(0) :

nest(itertools.repeat(0),[3,3,3])

回答by Dario

Just nest the multiplication syntax:

只需嵌套乘法语法:

[[[0] * 3] * 3] * 3

It's therefore simple to express this operation using folds

因此使用折叠来表达这个操作很简单

def zeros(dimensions):
    return reduce(lambda x, d: [x] * d, [0] + dimensions)

Or if you want to avoid reference replication, so altering one item won't affect any other you should instead use copies:

或者,如果您想避免引用复制,因此更改一项不会影响任何其他项目,您应该使用副本:

import copy
def zeros(dimensions):
    item = 0
    for dimension in dimensions:
        item = map(copy.copy, [item] * dimension)
   return item