如何在python中向3维数组添加元素

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

How to add elements to 3 dimensional array in python

pythonarraysmultidimensional-array

提问by lokesh

I am trying to store data in three-dimensional array i.e, x[0][0][0]in Python. How to initialize x, and add values to it? I have tried this:

我正在尝试将数据存储在三维数组中,即x[0][0][0]在 Python 中。如何初始化x,并为其添加值?我试过这个:

x=[]
x[0][0][0]=value1 
x[0][0].append(value1)

both lines are giving out of range error. How to do it? I want it like: x[0][0][0]=value1, x[1][0][0]=value2, x[0][1][0]=value3etc. How to achieve this in Python?

两条线都给出了超出范围的错误。怎么做?我想它像:x[0][0][0]=value1x[1][0][0]=value2x[0][1][0]=value3等。如何在Python实现这一目标?

I am looking to generate this kind of array:

我正在寻找生成这种数组:

x=[[[11,[111],[112]],[12],[13]],[[21,[211],[212]],[22],[23],[24]],[[31],[32]]]
x[0][0][0] will give 11
x[1][0][0]  21
x[0][0][1] 111

etc.

等等。

采纳答案by amaurea

I recommend using numpyfor multidimensional arrays. It makes it much more convenient, and much faster. This would look like:

我推荐numpy用于多维数组。它使它更方便,更快。这看起来像:

import numpy as np
x = np.zeros((10,20,30)) # Make a 10 by 20 by 30 array
x[0,0,0] = value1

Still, if you don't want to use numpy, or need non-rectangular multi-dimensional arrays, you will need to treat it as a list of lists of lists, and initialize each list:

尽管如此,如果您不想使用numpy,或需要非矩形多维数组,则需要将其视为列表列表的列表,并初始化每个列表:

x = []
x.append([])
x[0].append([])
x[0][0].append(value1)

Edit: Or you could use the compact notation shown in ndpu's answer (x = [[[value1]]]).

编辑:或者您可以使用 ndpu 的答案 ( x = [[[value1]]]) 中显示的紧凑符号。

回答by Andrew Walker

If you can use numpy, you can initialize a fixed size array as:

如果可以使用 numpy,则可以将固定大小的数组初始化为:

import numpy
x = numpy.zeros((i, j, k))

where i, j and k are the dimensions required.

其中 i、j 和 k 是所需的维度。

You can then index into that array using slice notation:

然后,您可以使用切片表示法对该数组进行索引:

x[0, 0, 0] = value1
x[1, 0, 0] = value2

回答by ndpu

>>> x=[[[[]]]]
>>> x[0][0][0]=0
>>> x
[[[0]]]
>>> x[0][0].append(1)
>>> x
[[[0, 1]]]

回答by HYRY

If you are creating some 3D sparse array, you can save all the data in a dict:

如果您正在创建一些 3D 稀疏数组,您可以将所有数据保存在一个 dict 中:

x={}
x[0,0,0] = 11
x[1,0,0] = 21
x[0,1,1] = 111

or:

或者:

from collections import defaultdict
x = defaultdict(lambda :defaultdict(lambda :defaultdict(int)))

x[0][0][0] = 11
x[1][0][0] = 21
x[0][0][1] = 111

回答by user2099484

A compilation of techniques above with respect to effectively creating a two-dimensional dictis:

上面关于有效创建二维字典的技术汇编是:

from collections import defaultdict
x = defaultdict(lambda :defaultdict())
x["a"]["b"] = 123
x["a"]["c"] = 234
x["b"]["a"] = 234    
x["b"]["c"] = 234    
x["c"]["a"] = 234
x["c"]["b"] = 234
for i in x: 
    for j in x[i]: print i, j, x[i][j]

Produces:

产生:

a c 234
a b 123
c a 234
c b 234
b a 234
b c 234

To increase the number of dimensions (e.g., to three-dimensions), simply increase the lambda "nest" as HYRY shows:

要增加维数(例如,增加到三个维度),只需增加 lambda“嵌套”,如 HYRY 所示:

x = defaultdict(lambda :defaultdict(lambda :defaultdict(int)))

回答by Peter Hausdorff Smith K.

Interator. I use function lambda for create matrix with [ [ 0 for j in range(n)] for i in range(m) ] in python 3. In the version 2 a used map functions. In this moment think like use array module (pure python) to create matrixes.

交互器。我使用函数 lambda 在 python 3 中使用 [ [ 0 for j in range(n)] for i in range(m) ] 创建矩阵。在版本 2 中使用了映射函数。在这一刻想像使用数组模块(纯python)来创建矩阵。

>>> arr = lambda m,n,l : [ [ [0 for k in range(l)] for j in range(n)] for i in range(m) ]
>>> m = arr(2,3,4)
>>> m
    [[[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 Wayne Workman

I just came up with this, it's more dynamic and simple.

我刚想出这个,它更动态和简单。

# Define how large to make the object.
size = 3

# Build the three dimensional list.
memory = []
for x in range(0,size):
    memory.append([])
    for y in range(0,size):
        memory[x].append([])
        for z in range(0,size):
           memory[x][y].append(0) # Fill with zeros.

# Iterate through all values.
for x in range(0,size):
    for y in range(0,size):
        for z in range(0,size):
            print 'memory[' + str(x) + '][' + str(y) + '][' + str(z) + ']=' + str(memory[x][y][z])

# Example access.
print 'Example access:'
print 'memory[0][1][2]=' + str(memory[0][1][2])

Output:

输出:

memory[0][0][0]=0
memory[0][0][1]=0
memory[0][0][2]=0
memory[0][1][0]=0
memory[0][1][1]=0
memory[0][1][2]=0
memory[0][2][0]=0
memory[0][2][1]=0
memory[0][2][2]=0
memory[1][0][0]=0
memory[1][0][1]=0
memory[1][0][2]=0
memory[1][1][0]=0
memory[1][1][1]=0
memory[1][1][2]=0
memory[1][2][0]=0
memory[1][2][1]=0
memory[1][2][2]=0
memory[2][0][0]=0
memory[2][0][1]=0
memory[2][0][2]=0
memory[2][1][0]=0
memory[2][1][1]=0
memory[2][1][2]=0
memory[2][2][0]=0
memory[2][2][1]=0
memory[2][2][2]=0
Example access:
memory[0][1][2]=0