【问题标题】:How to iterate over this n-dimensional dataset?如何迭代这个 n 维数据集?
【发布时间】:2018-01-26 00:46:36
【问题描述】:

我有一个dataset,它有 4 个维度(目前...),我需要对其进行迭代。

要访问dataset 中的值,我这样做:

value = dataset[i,j,k,l]

现在,我可以为dataset 获取shape

shape = [4,5,2,6]

shape 中的值代表维度的长度。

在给定维数的情况下,我如何迭代数据集中的所有元素?这是一个例子:

for i in range(shape[0]):
    for j in range(shape[1]):
        for k in range(shape[2]):
            for l in range(shape[3]):
                print('BOOM')
                value = dataset[i,j,k,l]

将来,shape 可能会发生变化。例如,shape 可能有 10 个元素,而不是当前的 4 个。

在 Python 3 中是否有一种简洁明了的方式来做到这一点?

【问题讨论】:

标签: python multidimensional-array iteration


【解决方案1】:

您可以使用itertools.product 迭代某些值(在本例中为索引)的cartesian product 1

import itertools
shape = [4,5,2,6]
for idx in itertools.product(*[range(s) for s in shape]):
    value = dataset[idx]
    print(idx, value)
    # i would be "idx[0]", j "idx[1]" and so on...

但是,如果它是您想要迭代的 numpy 数组,可能更容易使用np.ndenumerate

import numpy as np

arr = np.random.random([4,5,2,6])
for idx, value in np.ndenumerate(arr):
    print(idx, value)
    # i would be "idx[0]", j "idx[1]" and so on...

1 您要求澄清itertools.product(*[range(s) for s in shape]) 的实际作用。所以我会更详细地解释它。

例如你有这个循环:

for i in range(10):
    for j in range(8):
        # do whatever

这也可以使用product 编写为:

for i, j in itertools.product(range(10), range(8)):
#                                        ^^^^^^^^---- the inner for loop
#                             ^^^^^^^^^-------------- the outer for loop
    # do whatever

这意味着product 只是减少独立 for 循环数量的便捷方式。

如果您想将可变数量的for-loops 转换为product,您基本上需要两个步骤:

# Create the "values" each for-loop iterates over
loopover = [range(s) for s in shape]

# Unpack the list using "*" operator because "product" needs them as 
# different positional arguments:
prod = itertools.product(*loopover)

for idx in prod:
     i_0, i_1, ..., i_n = idx   # index is a tuple that can be unpacked if you know the number of values.
                                # The "..." has to be replaced with the variables in real code!
     # do whatever

相当于:

for i_1 in range(shape[0]):
    for i_2 in range(shape[1]):
        ... # more loops
            for i_n in range(shape[n]):  # n is the length of the "shape" object
                # do whatever

【讨论】:

  • 你不需要。使用[i, j, k, l] 进行索引等同于[(i, j, k, l)],而idx 只是(i, j, k, l),因此您可以使用dataset[idx] 对其进行索引(如图所示)。 :)
  • 如果它是一个 ndarray 并且您想在不迭代索引的情况下对其进行迭代,您可以使用 ndarray.nditer 而不是 ndarray.ndenumerate
  • 更正,这些是numpy的函数而不是ndarray的函数:ndenumerate,nditer
猜你喜欢
  • 2012-12-11
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多