【问题标题】:find a list of row position of last true value in each column查找每列中最后一个真值的行位置列表
【发布时间】:2020-08-13 04:53:22
【问题描述】:

我有以下代码:

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, 11, ....]

或者无论如何要从curve_list中获取值的位置,它是小于或等于dtm中值的值的最大值?

谢谢

【问题讨论】:

  • 定义变量的值
  • 在我的示例中,dtm 中的值是 [10, 9.96, 9.92, ......]。通过将 dtm 与 curve_list 进行比较,10(来自 curve_list 的第 12 个位置)是 dtm 中小于或等于 10 的最高值。而 9(curve_list 中的第 11 位)是比 9.96、9.92、.... 来自 dtm 的最大的小值。
  • 您的代码中的column_listpos 是什么?我很乐意为您提供更多信息
  • 很抱歉我的错字。我编辑了这个问题。 column_list 是curve_list,pos 是pos1。

标签: python numpy


【解决方案1】:

扩展您的代码:

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]
'''

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多