简体   繁体   中英

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.

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?

Ideally, the return would be [[i,j,value],......]

I have tried enumerate() but not sure how to implement it correctly. 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 ).

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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