查找每列中最后一个真值的行位置列表

2024-10-01 00:27:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

ttmbond = 10
daywalk = np.arange(0,30)
dtm = ttmbond - daywalk/252 

curve_list = [0.083,0.25,0.5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

pos1= np.ones((len(daywalk+1),len(curve_list)))
pos2 = pos1*curve_list

pos3 = pos2 <= dtm

这给了我这个 TRUE/FALSE ndarray 我想得到每列中最后一个真值的行索引列表。从这个例子中,我的最终结果应该是[12,11,11,11,11,…]

或者,是否可以从曲线列表中获取值的位置,该列表是dtm中值的最大值,小于或等于该值

谢谢


Tags: 代码true列表lennponeslistcurve
1条回答
网友
1楼 · 发布于 2024-10-01 00:27:47

扩展代码:

ttmbond = 10
daywalk = np.arange(0,30)
dtm = ttmbond - daywalk/252 

curve_list = [0.083,0.25,0.5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

pos1= np.ones((len(daywalk+1),len(curve_list)))
pos2 = pos1*curve_list

pos3 = (pos2 <= (dtm+pos1.T).T)

temp = np.where(pos3==True)
loc = np.where((temp[0][1:]-temp[0][:-1])==1)[0]
res = np.append(temp[1][loc], temp[1][-1])
print(res)

'''
Output:
[13 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12
 12 12 12 12 12 12]
'''

另一方面:

ttmbond = 10
daywalk = np.arange(0,30)
dtm = ttmbond - daywalk/252 

curve_list = [0.083,0.25,0.5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

res = [np.where((i>0)==True)[0][0] for i in [curve_list - i for i in dtm]]
print(res)

'''
Output:
[13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12]
'''

相关问题 更多 >