简体   繁体   English

从数组中返回索引和值

[英]return index and value from array

So, the goal here is to iterate through each row of a DEM (or any spatially referenced array), find the smallest value (eg low point), and then return the value and the related index. 因此,这里的目标是遍历DEM(或任何空间引用的数组)的每一行,找到最小值(例如低点),然后返回值和相关索引。

This is not elegant, but I can get the values easily enough with a loop and seed: 这不是很优雅,但我可以通过循环和种子轻松获得值:

`lowpts=[]
low=99999
for i in range(len(DEM)):
    for j in range(len(DEM)):
        low1 = DEM[i][j]
        if low1 < low:
            low = low1
    lowpts.append(low)`

But now how do I retain the [i][j] index associated with each value? 但是现在如何保留与每个值相关的[i] [j]索引?

Ideally, the return would be [[i,j,value],......] 理想情况下,回报将是[[i,j,value],......]

I have tried enumerate() but not sure how to implement it correctly. 我尝试过enumerate()但不确定如何正确实现它。 And I have to be aware of possible duplicate values, so I cannot just .index the lowpts array (akin to Python: finding an element in an array ). 我必须知道可能的重复值,所以我不能只是.index lowpts数组(类似于Python:在数组中查找元素 )。

you are giving the answer in your own question! 你在自己的问题中给出答案!

lowpts=[]
low=99999
for i in range(len(DEM)):
    for j in range(len(DEM)):
        low1 = DEM[i][j]
        if low1 < low:
            low = low1
            low_i = i
            low_j = j
    lowpts.append([i,j,low])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM