我有這個矩陣;假設這個矩陣中有數百個 x 值和數千個 y 串列:
[[100 0 0 ... 0 0 0]
[ 0 100 0 ... 0 0 0]
[ 0 0 100 ... 0 0 0]
...
[ 0 0 0 ... 100 0 0]
[ 0 0 0 ... 0 100 0]
[ 0 0 0 ... 0 0 100]]
我將如何能夠檢索索引value >= 90并將其放入嵌套串列中?
這是示例輸出:
[[0], [1, 167], [2, 498, 2890] ... [6568, 99998], [7894, 19695, 99999], [873, 100000]]
uj5u.com熱心網友回復:
嘗試使用嵌套串列理解:
[[idx for idx, value in enumerate(row) if value >= 90] for row in arr]
要獲得@azro 的結果,我寧愿使用:
np.argwhere(arr >= 90)
uj5u.com熱心網友回復:
后numpy get index where value is true可能會有所幫助
numpy.transpose((array > value).nonzero())
import numpy as np
values = np.array([[1, 76, 987, 1, 1, 1], [876, 76, 765, 1, 1, 1], [1, 3, 4, 1, 1, 1],
[1, 3, 4, 1, 1, 1], [1, 3, 4, 1, 1, 1], [1, 3, 4, 1, 1, 123]])
result = np.transpose((values > 90).nonzero())
print(result)
[[0 2]
[1 0]
[1 2]
[5 5]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/390778.html
上一篇:是否有比較串列中專案的功能?
下一篇:flake8在毒物測驗環境中失敗
