python嵌套列表格式,搜索大于或最新的值
下面是我的数据python嵌套列表格式,搜索大于或最新的值,python,formatting,nested-lists,Python,Formatting,Nested Lists,下面是我的数据 a = [{'ID': 319684283, 'ID1': 1025018, 'date': '2018-01-07 17:39:46', 'rate': 9.639e-05, 'amount': 410.84392747, 'total': 0.03960124, 'order': 16532584965, 'type': 'A', 'category': 'website'}, {'ID': 319684282, 'ID1': 1025017, 'date': '201
a = [{'ID': 319684283, 'ID1': 1025018, 'date': '2018-01-07 17:39:46', 'rate': 9.639e-05, 'amount': 410.84392747, 'total': 0.03960124, 'order': 16532584965, 'type': 'A', 'category': 'website'}, {'ID': 319684282, 'ID1': 1025017, 'date': '2018-01-07 17:39:46', 'amount': 24.84386425, 'total': 0.00239445, 'order': 16532584965, 'type': 'phone', 'category': 'exchange'}, {'ID': 319684281, 'ID1': 1125117, 'date': '2018-01-17 17:39:16', 'amount': 14.8138145, 'total': 0.10239445, 'order': 16512581965, 'type': 'phone', 'category': 'exchange'}]
如何提取最新的记录。我试过了
for c in a:
print(min(c['date']
它失败了,因为它无法遍历所有元素。
如何实现以下输出限制值:
d = { 'date': '2018-01-17 17:39:16', 'amount': 14.8138145, 'total': 0.10239445, 'order': 16512581965}
此处d应仅具有基于日期的最新值 您需要遍历每个列表,然后从字典中获取日期以找到最小日期。一旦你找到了最短的日期,你就会拥有你的价值
min_date = "2222-01-01 00:00:00"
for dict in data_list:
date = dict.get("date")
if date < min_date:
value = dict
min_date=“2222-01-01 00:00:00”
对于数据列表中的dict:
日期=dict.get(“日期”)
如果日期<最小日期:
值=dict
为什么它不能遍历所有元素?d=max(a,key=lambda d:d['date'])
。因为您的日期是ISO8601字符串,所以对字符串进行排序可以让您通过这种方式获得“最新”日期。