python 迭代 3D 数组的 Pythonic 方式

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

Pythonic way of iterating over 3D array

pythonarraysloops

提问by Nathan Fellman

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:

我在 Python 中有一个 3D 数组,我需要遍历数组中的所有多维数据集。也就是说,对于(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. I just found it very easy to read the data in with a one-liner using numpy.fromfile().

该数组是一个 Numpy 数组,尽管这并不是必需的。我刚刚发现使用numpy.fromfile().

Is there any more Pythonic way to iterate over these than the following? That simply looks like C using Python syntax.

有没有比以下更多的 Pythonic 方法来迭代这些?这看起来就像使用 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)])

回答by Otto Allmendinger

Have a look at itertools, especially itertools.product. You can compress the three loops into one with

看看itertools,尤其是itertools.product。您可以将三个循环压缩为一个

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

cube + (x,y,z)在你的情况下会转化为。您的代码的非常紧凑的版本将是

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.productmakes the product over the different arguments, i.e. itertools.product(a,b,c), so I have to pass map(xrange, ...)with as *map(...)

编辑itertools.product使产品通过不同的参数,即itertools.product(a,b,c),所以我必须通过map(xrange, ...)as*map(...)

回答by nosklo

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])