如何在python多维列表中查找高于特定值的所有索引
问题描述:
我在python中有一个带有一些浮点值的二维列表.我想找出列表中所有高于特定值的元素的索引.
I have a two dimensional list in python with some floating point values. I want to find out indices of all the elements in the list which are above a specific value.
例如,在下面的2 x 4列表中(为方便起见,为整数):
For example, in a 2 x 4 list as below (integer for convenience):
100 200 100 250
125 100 250 100
我需要所有等于或大于200的值的索引.这些索引是[0] [1],[0] [3]和[1] [2].
I need the indices of all the values which are equal or above 200. These indices are [0][1], [0][3] and [1][2].
请在这方面提供帮助.
谢谢.
答
Numpy对此非常方便:
Numpy is quite handy for this:
>>> import numpy as np
>>> a = [[100, 200, 100, 250], [125, 100, 250, 100]]
>>> a=np.array(a)
>>> np.argwhere(a>=200)
array([[0, 1],
[0, 3],
[1, 2]])