我试图为当前列表(日期和值)获取最近日期的列表。
有两份清单:
[["20160901", 0.244], ["20160902", 0.232], ["20160906", 0.214],
["20160909", 0.235], ["20160910", 0.244], ["20160911", 0.271],
["20160912", 0.239], ["20160914", 0.25], ...]长度为X
以及:
[["20160907", -9.39979076385498, -6.318868160247803],
["20160913", -10.568793296813965, -6.815752029418945], ... ]长度是Y
需要得到的第一个列表的长度是Y和最近的日期为第二个列表。
例如:
[["20160906", 0.214], ["20160912", 0.239], ...]这是我的代码:
def nearest_date(dates, pivot):
return min(dates, key=lambda x: abs(x - pivot))但我有不同尺寸的单子。
发布于 2016-09-30 08:53:20
list_dates = [["20160901", 0.244], ["20160902", 0.232], ["20160906", 0.214],
["20160909", 0.235], ["20160910", 0.244], ["20160911", 0.271],
["20160912", 0.239], ["20160914", 0.25]]
list_comp = [["20160907", -9.39979076385498, -6.318868160247803],
["20160913", -10.568793296813965, -6.815752029418945]]
[ min(list_dates, key = lambda x: abs(int(x[0])-int(p[0]))) for p in list_comp ]结果:
[['20160906', 0.214], ['20160912', 0.239]]https://stackoverflow.com/questions/39786772
复制相似问题