【发布时间】:2017-07-25 19:13:54
【问题描述】:
我有一个温度列表,我需要在所需温度之前和之后找到 n 个值,最好在单独的列表中。我列表中的值不一定是唯一的,但我需要原始列表的索引。我需要这些索引来查找其他列表中的其他参数。
例子:
TestArray = np.array([12,42,19,32,41,14,17,23,12,18,32,19])
Value = 20
n = 2
TestArray = np.append(TestArray, Value)
Sort = np.argsort(TestArray)
Index = np.where(Sort == (len(TestArray)-1))[0][0]
Lower = Sort[Index-n:Index]
Upper = Sort[Index+1:Index+n+1]
print(Upper, TestArray[Upper])
print(Lower, TestArray[Lower])
我的代码给出了想要的输出,但它看起来真的很乱,我想知道是否有更好的方法。
说明: 我将想要的值附加到末尾,所以我知道它的索引。然后我使用 argsort 从低到高查找索引,然后使用 np.where 定位我想要的值在哪里。然后使用索引查找上限值和下限值。
【问题讨论】: