Vestlnd:
このようなリストでは:
[10, 11, 12,np.nan, 14, np.nan, 16, 17, np.nan, 19, np.nan]
どのように2不足している(との間に配置されている値サブセットすることができますnan
)の値を?
私はそれを行う方法を知っていますfor loop
:
# imports
import numpy as np
# input
lst=[10,11,12,np.nan, 14, np.nan, 16, 17, np.nan, 19, np.nan]
# define an empty list and build on that in a For Loop
subset=[]
for i, elem in enumerate(lst):
if np.isnan(lst[i-1]) and np.isnan(lst[i+1]):
subset.extend([elem])
print(subset)
# output
# [14, 19]
あまり面倒な方法でこれを行う方法上の任意の提案?
永遠の :
使用リストの内包
import numpy as np
lst=[10,11,12,np.nan, 14, np.nan, 16, 17, np.nan, np.nan, np.nan]
subset = [elem for i, elem in enumerate(lst) if i and i < len(lst)-1 and np.isnan(lst[i-1]) and np.isnan(lst[i+1]) and not np.isnan(elem)]
print(subset)
他の貢献者によって指摘されたミスを修正しました。これは今、すべてのケースのために働く必要があります。