我有多个词典,我想获取日期最早的分支的“最早的日期”和“名称”
我可以使用此日期获取最旧的日期,但是无法获取哪个分支具有最旧的日期。
x = datetime.datetime.now()
# objects
branch_1 = {
"name": "b1",
"gps": (48.8946865, 2.3622423),
"oldestDate": dt.datetime(2019, 1, 7),
}
branch_2 = {
"name": "b2",
"gps": (48.839955, 2.288605),
"cars": 7,
"oldestDate": dt.datetime(2016, 1, 17),
}
branch_3 = {
"name": "b3",
"gps": (48.844244, 2.401435),
"oldestDate": dt.datetime(2019, 1, 21),
}
listOBranches = [branch_1, branch_2, branch_3]
mtlst2 = []
def branchPriorityScore(listOBranches):
for item in listOBranches:
score = x - (item["oldestDate"])
mtlst2.append(score)
dateMax2 = np.max(mtlst2)
name = item["name"]
# return mtlst2
print("Maximum Priority Branch : ", dateMax2) # correct score
# print("Maximum Priority Branch : ", np.max(mtlst2(item["name"])), dateMax2)
# mtlst2
branchPriorityScore(listOBranches)
答案 0 :(得分:0)
我会这样做的。
def branchPriorityScore(listOBranches):
oldest = datetime.timedelta()
i = 0 #if you need index
for index,item in enumerate(listOBranches):
score = x - (item["oldestDate"])
if score > oldest:
oldest = score
i = index #if you need index
name = item["name"]
print (name,i,oldest,listOBranches[i]['oldestDate'])
不确定为什么需要mtlst2,但也可以在循环中添加它。