简体   繁体   English

迭代3D数组的Pythonic方式

[英]Pythonic way of iterating over 3D array

I have a 3D array in Python and I need to iterate over all the cubes in the array. 我在Python中有一个3D数组,我需要迭代数组中的所有多维数据集。 That is, for all (x,y,z) in the array's dimensions I need to access the cube: 也就是说,对于数组维度中的所有(x,y,z) ,我需要访问多维数据集:

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. 该数组是一个Numpy数组,虽然这不是必需的。 I just found it very easy to read the data in with a one-liner using numpy.fromfile() . 我刚刚发现使用numpy.fromfile()使用numpy.fromfile()读取数据非常容易。

Is there any more Pythonic way to iterate over these than the following? 是否有更多的Pythonic方法来迭代这些而不是以下? That simply looks like C using Python syntax. 这简直就像使用Python语法的C一样。

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 . 看一下itertools ,特别是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. 在你的情况下,它将转换为cube + (x,y,z) 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, ie itertools.product(a,b,c) , so I have to pass map(xrange, ...) with as *map(...) 编辑itertools.product使产品通过不同的参数,即itertools.product(a,b,c) ,所以我必须使用as *map(...)传递map(xrange, ...) *map(...)

import itertools
for x, y, z in itertools.product(xrange(x_size), 
                                 xrange(y_size), 
                                 xrange(z_size)):
    work_with_cube(array[x, y, z])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM