嗨,我想使用另一个数组中的值,使用元组列表作为索引
代码 :
import numpy as np
elevation_array = np.random.rand(5,5) #creates a random array 5 by 5
sort_idx = np.argsort(elevation_array, axis=None)
new_idx = zip(*np.unravel_index(sort_idx[::-1], elevation_array.shape))
for r, c in new_idx:
r, c = [r, c]
for [x, y], elevation in np.ndenumerate(elevation_array):
print elevation[r, c] # I will want to for other stuff here later on
我也试过这样:
for r, c in new_idx:
r, c = [r, c]
for elevation in np.ndenumerate(elevation_array):
print elevation[r, c]
我在第一个中得到错误:
IndexError: 0-d arrays can only use a single () or a list of newaxes (and a single ...) as an index
任何帮助都会很棒,而且解释会非常有用,因为我是 python 新手
在第二个我得到错误:
tuple indices must be integers, not tuple
回答:
for r, c in new_idx:
print elevation_array[r, c]
我得到它是如此简单,我不敢相信我不知道该怎么做!:)