1

嗨,我想使用另一个数组中的值,使用元组列表作为索引

代码 :

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] 

我得到它是如此简单,我不敢相信我不知道该怎么做!:)

4

1 回答 1

0

你可以得到相同的结果:

print np.sort(elevation_array.flat)[::-1]
于 2013-08-04T12:57:00.857 回答