访问多维数组的每个元素,而不需要知道各个维度。

huangapple go评论79阅读模式
英文:

Access each element of a multidimensional array without knowing the individual dimensions

问题

import numpy as np
import random

myRand = np.random.rand(5, 6, 7)

这段代码适用于已知myRand具有3个维度的情况。如果我不知道呢?

mySum = 0.0
for i in range(myRand.shape[0]):
for j in range(myRand.shape[1]):
for k in range(myRand.shape[2]):
# 对myRand[i,j,k]执行某些操作

英文:

I have a multidimensional array but I won't know the number of dimensions or the size of each dimension. How can I generalize the code such that I can access each element of the array individually?

import numpy as np
import random
    
myRand = np.random.rand(5, 6, 7)
    
#print (myRand.shape[0])
# This works great if I already know that myRand has 3 dimensions. What if I don't know that?
mySum = 0.0
for i in range(myRand.shape[0]):
    for j in range(myRand.shape[1]):
        for k in range(myRand.shape[2]):
#           Do something with myRand[i,j,k]

答案1

得分: 1

如果您不需要在计算中保留每个维度内的索引,您可以只使用numpy.nditer

a = np.arange(8).reshape((2, 2, 2))
a
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

for i in np.nditer(a):
    print(i)

# 输出结果:
0
1
2
3
4
5
6
7

您通常不应该需要像这样使用for循环迭代数组。通常使用numpy的方法来执行您要进行的计算会更好。

英文:

If you do not need to retain the indices within each of the dimensions for calculation, you can just use numpy.nditer

>>> a = np.arange(8).reshape((2, 2, 2))
>>> a
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

>>> for i in np.nditer(a):
...     print(i)
...
0
1
2
3
4
5
6
7

> You shouldn't really need to iterate over the array using a for-loop like this. There is usually a better way to perform whatever computation you are doing using numpy methods

答案2

得分: 1

你可以使用[itertools][1]来完成这个任务。

以下是生成索引的代码,通过这些索引你可以访问数组中的元素:

import numpy as np
import itertools

v1 = np.random.randint(5, size=2)
v2 = np.random.randint(5, size=(2, 4))
v3 = np.random.randint(5, size=(2, 3, 2))

# v1
args1 = [list(range(e)) for e in list(v1.shape)]
print(v1)
for combination in itertools.product(*args1):
    print(v1[combination])

# v2
args2 = [list(range(e)) for e in list(v2.shape)]
print(v2)
for combination in itertools.product(*args2):
    print(v2[combination])

# v3
args3 = [list(range(e)) for e in list(v3.shape)]
print(v3)
for combination in itertools.product(*args3):
    print(v3[combination])

在不同大小的简单数组上进行了测试运行正常

[1]: https://docs.python.org/2/library/itertools.html

<details>
<summary>英文:</summary>

You could use [`itertools`][1] to do so.

the following piece of code will generate the indices which you will be able to access the array while obtaining them:

    import numpy as np
    import itertools
    
    v1 = np.random.randint(5, size=2)
    v2 = np.random.randint(5, size=(2, 4))
    v3 = np.random.randint(5, size=(2, 3, 2))
    
    # v1
    args1 = [list(range(e)) for e in list(v1.shape)]
    print(v1)
    for combination in itertools.product(*args1):
        print(v1[combination])
    
    # v2
    args2 = [list(range(e)) for e in list(v2.shape)]
    print(v2)
    for combination in itertools.product(*args2):
        print(v2[combination])
        
    # v3
    args3 = [list(range(e)) for e in list(v3.shape)]
    print(v3)
    for combination in itertools.product(*args3):
        print(v3[combination])

Tested it on a simple arrays with different sizes and it works fine.




  [1]: https://docs.python.org/2/library/itertools.html

</details>



huangapple
  • 本文由 发表于 2020年1月6日 22:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定