如何添加元素在Python 3维数组 [英] How to add elements to 3 dimensional array in python

查看:213
本文介绍了如何添加元素在Python 3维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据存储在三维阵列即,X [0] [0] [0]蟒蛇。如何初始化的X.并增加值到它。我想这一点。

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 tried this.

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

这两个行给予了一系列的错误。怎么做。我想它喜欢。 X [0] [0] [0] =值,X [1] [0] [0] =值2,X [0] [1] [0] =值3等如何在Python实现这一目标。

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]=value3 etc. how to achieve this in 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

推荐答案

我推荐使用 numpy的多维数组。这使得它更方便,速度更快。这看起来像

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

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

不过,如果你不想使用 numpy的,或需要非矩形的多维数组,你需要把它当作列表的列表列表,并初始化每个列表:

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:

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

编辑:或者你可以使用ndpu的回答显示的紧凑型符号(X = [[[值1]]])

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

这篇关于如何添加元素在Python 3维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆