遍历数组3D的Python的方式 [英] Pythonic way of iterating over 3D array
问题描述
我在Python中的三维数组,我需要通过遍历数组中的所有多维数据集。也就是说,对于所有的(X,Y,Z)
在阵列的尺寸,我需要访问立方体:
数组[(X + 0,Y + 0,Z + 0)]
数组[(X + 1,Y + 0,Z + 0)]
数组[(X + 0,Y + 1,Z + 0)]
数组[(X + 1,Y + 1,Z + 0)]
数组[(X + 0,Y + 0,Z + 1)]
数组[(X + 1,Y + 0,Z + 1)]
数组[(X + 0,Y + 1,Z + 1)]
数组[(X + 1,Y + 1,Z + 1)]
该阵列是一个numpy的数组,尽管这不是真的有必要。我只是发现它很容易用一个班轮使用读取数据 numpy.fromfile()
。
有没有遍历这些比以下任何一个更Python的方式?简单地使用Python语法看起来像C。
在范围X(x_dimension):
在范围(y_dimension)Y:
在范围(z_dimension)Z:
work_with_cube(数组[(X + 0,Y + 0,Z + 0)],
数组[(X + 1,Y + 0,Z + 0)],
数组[(X + 0,Y + 1,Z + 0)],
数组[(X + 1,Y + 1,Z + 0)],
数组[(X + 0,Y + 0,Z + 1)],
数组[(X + 1,Y + 0,Z + 1)],
数组[(X + 0,Y + 1,Z + 1)],
数组[(X + 1,Y + 1,Z + 1)])
看一看和itertools ,特别是 itertools.product 。您可以COM preSS三环路成一个以
导入和itertools为X,Y,Z在itertools.product(*地图(的xrange(x_dim,y_dim,z_dim)):
...
您还可以创建多维数据集是这样的:
立方= numpy.array(名单(itertools.product((0,1),(0,1),(0,1))))
打印立方
阵列([0,0,0],
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
和一个简单的加法添加偏移量
打印立方体+(10,100,1000)
阵列([10,100,1000],
[10,100,1001],
[10,101,1000],
[10,101,1001〕,
[11,100,1000]
[11,100,1001],
[11,101,1000],
[11,101,1001]])
翻译为立方体+(X,Y,Z)
你的情况这将。您code的非常紧凑的版本是
导入和itertools,numpy的立方体= numpy.array(列表(itertools.product((0,1),(0,1),(0,1))))x_dim = y_dim = z_dim = 10在itertools.product偏移量(*地图(的xrange(x_dim,y_dim,z_dim))):
work_with_cube(立方体+偏移)
修改: itertools.product
使得该产品在不同的参数,即 itertools.product(A,B ,C)
,所以我必须要通过地图(的xrange,...)
与作为 *地图(.. 。)
I have a 3D array in Python and I need to iterate over all the cubes in the array. That is, for all (x,y,z)
in the array's dimensions I need to access the cube:
array[(x + 0, y + 0, z + 0)]
array[(x + 1, y + 0, z + 0)]
array[(x + 0, y + 1, z + 0)]
array[(x + 1, y + 1, z + 0)]
array[(x + 0, y + 0, z + 1)]
array[(x + 1, y + 0, z + 1)]
array[(x + 0, y + 1, z + 1)]
array[(x + 1, y + 1, z + 1)]
The array is a Numpy array, though that's not really necessary. I just found it very easy to read the data in with a one-liner using numpy.fromfile()
.
Is there any more Pythonic way to iterate over these than the following? That simply looks like C using Python syntax.
for x in range(x_dimension):
for y in range(y_dimension):
for z in range(z_dimension):
work_with_cube(array[(x + 0, y + 0, z + 0)],
array[(x + 1, y + 0, z + 0)],
array[(x + 0, y + 1, z + 0)],
array[(x + 1, y + 1, z + 0)],
array[(x + 0, y + 0, z + 1)],
array[(x + 1, y + 0, z + 1)],
array[(x + 0, y + 1, z + 1)],
array[(x + 1, y + 1, z + 1)])
Have a look at itertools, especially itertools.product. You can compress the three loops into one with
import itertools
for x, y, z in itertools.product(*map(xrange, (x_dim, y_dim, z_dim)):
...
You can also create the cube this way:
cube = numpy.array(list(itertools.product((0,1), (0,1), (0,1))))
print cube
array([[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]])
and add the offsets by a simple addition
print cube + (10,100,1000)
array([[ 10, 100, 1000],
[ 10, 100, 1001],
[ 10, 101, 1000],
[ 10, 101, 1001],
[ 11, 100, 1000],
[ 11, 100, 1001],
[ 11, 101, 1000],
[ 11, 101, 1001]])
which would to translate to cube + (x,y,z)
in your case. The very compact version of your code would be
import itertools, numpy
cube = numpy.array(list(itertools.product((0,1), (0,1), (0,1))))
x_dim = y_dim = z_dim = 10
for offset in itertools.product(*map(xrange, (x_dim, y_dim, z_dim))):
work_with_cube(cube+offset)
Edit: itertools.product
makes the product over the different arguments, i.e. itertools.product(a,b,c)
, so I have to pass map(xrange, ...)
with as *map(...)
这篇关于遍历数组3D的Python的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!