下面有一个字典列表
rec=[{
'Name': 'aRe',
'Email': 'huul@gmail.com',
'timestamp': '2021-11-29T04:33:28.138522Z'
},
{
'Name': 'Umar',
'Email': 'chdtn@gmail.com',
'timestamp': '2021-11-28T04:33:28.138522Z'
},
{
'Name': 'Are',
'Email': 'ddhit@gmail.com',
'timestamp': '2021-11-27T04:33:28.138522Z'
},
{
'Name': 'arE',
'Email': 'deyan@gmail.com',
'timestamp': '2021-11-28T06:59:58.975864Z'
},
{
'Name': 'umaR',
'Email': 'ddhi@gmail.com',
'timestamp': '2021-11-29T04:33:28.138522Z'
},
{
'Name': 'Sc',
'Email': 'deyan@gmail.com',
'timestamp': '2022-02-01T15:02:12.301701Z'
}
]
- 如果存在重复id,则提取具有最新时间戳的字典
预计出来
[{'Name': 'umaR',
'Email': 'ddhi@gmail.com',
'timestamp': '2021-11-29T04:33:28.138522Z'},
{'Name': 'aRe',
'Email': 'huul@gmail.com',
'timestamp': '2021-11-29T04:33:28.138522Z'},
{'Name': 'Sc',
'Email': 'deyan@gmail.com',
'timestamp': '2022-02-01T15:02:12.301701Z'}]
代码在
下面from itertools import groupby
filtered_recs = []
for key, group_iter in groupby(recs, lambda rec: rec['Name'].lower()):
recent_rec = max(group_iter, key = lambda rec: rec['timestamp'])
filtered_recs.append(recent_rec)
filtered_recs
我的代码工作良好,如果所有的'名称'在同一情况下。像name
一样,'are', 'umar', 'sc'不适用不规则大小写字母
先对recs
排序:
from itertools import groupby
filtered_recs = []
recs = sorted(recs, key=lambda rec: rec["Name"].lower()) # <-- sort before groupby
for key, group_iter in groupby(recs, lambda rec: rec["Name"].lower()):
recent_rec = max(group_iter, key=lambda rec: rec["timestamp"])
filtered_recs.append(recent_rec)
print(filtered_recs)
打印:
[
{
"Name": "aRe",
"Email": "huul@gmail.com",
"timestamp": "2021-11-29T04:33:28.138522Z",
},
{
"Name": "Sc",
"Email": "deyan@gmail.com",
"timestamp": "2022-02-01T15:02:12.301701Z",
},
{
"Name": "umaR",
"Email": "ddhi@gmail.com",
"timestamp": "2021-11-29T04:33:28.138522Z",
},
]
EDIT: Version without sort:
filtered_recs = {}
for r in recs:
filtered_recs.setdefault(r["Name"].lower(), []).append(r)
for k, v in filtered_recs.items():
filtered_recs[k] = max(v, key=lambda rec: rec["timestamp"])
print(list(filtered_recs.values()))
打印:
[
{
"Name": "aRe",
"Email": "huul@gmail.com",
"timestamp": "2021-11-29T04:33:28.138522Z",
},
{
"Name": "umaR",
"Email": "ddhi@gmail.com",
"timestamp": "2021-11-29T04:33:28.138522Z",
},
{
"Name": "Sc",
"Email": "deyan@gmail.com",
"timestamp": "2022-02-01T15:02:12.301701Z",
},
]