我有以下陣列,時間和通量:
Time = np.array(lc2.time.value[transit_mask])
print('Time =',Time)
Flux = np.array(lc2.sap_flux.value[transit_mask])
print('Flux =', Flux)
他們給了我以下陣列:
Time = [2420.69047176 2420.69186069 2420.69324961 2420.69463854 2420.69602747
2420.69741639 2420.69880532 2420.70019425 2420.70158318 2420.7029721
2420.70436103 2420.70574996 2420.70713888 2420.70852781 2420.70991674]
Flux = [3655.0168 3648.3088 3660.2622 3651.3274 3657.7986 3664.8052 3666.8796
3665.003 3660.034 3654.9521 3651.9277 3652.042 3668.4712 3659.576
3657.6863]
如果我有陣列中的值2420.70019425,Time如何在陣列中找到相應的值Flux?
(我嘗試了許多可能的解決方案,但我也收到以下索引錯誤訊息IndexError: only integers, slices (:: ), ellipsis (... ), numpy.newaxis (None ) and integer or boolean arrays are valid indices)
謝謝!
uj5u.com熱心網友回復:
idx = np.argwhere(Time == 2420.70019425)
print(Flux[idx[0]])
但是,如果您這樣做,請確保您確實有來自 Time 的元素,而不是文字值。您無法可靠地比較浮點值是否相等。它們都只是近似值。
可運行示例:
import numpy as np
Time = np.array([2420.69047176, 2420.69186069, 2420.69324961, 2420.69463854, 2420.69602747,
2420.69741639, 2420.69880532, 2420.70019425, 2420.70158318, 2420.7029721,
2420.70436103, 2420.70574996, 2420.70713888, 2420.70852781, 2420.70991674])
Flux = np.array([3655.0168, 3648.3088, 3660.2622, 3651.3274, 3657.7986, 3664.8052, 3666.8796,
3665.003, 3660.034, 3654.9521, 3651.9277, 3652.042, 3668.4712, 3659.576,
3657.6863])
val = Time[12]
idx = np.argwhere(Time==val)
print(idx)
print(Flux[idx[0]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/434350.html
上一篇:如何將兩個陣列傳遞給一個函式
