我有一个字典列表。
my_list = [
{"id": "UU7t", "updated_at": "2020-01-06_16-40-00", "summary": "Renewed"},
{"id": "yT8h", "updated_at": "2020-01-07_18-24-22", "summary": "Renewed"},
{"id": "i8Po", "updated_at": "2020-01-08_13-16-36", "summary": "Renewed"},
{"id": "yT8h", "updated_at": "2020-01-13_18-24-05", "summary": "Deleted"},
{"id": "7uYg", "updated_at": "2020-01-18_23-37-19", "summary": "Transferred"},
]
我想获取已删除重复字典的列表,其中 id 相同但“updated_at”是最新的。
所以,我的最终 list 是:
my_list = [
{"id": "UU7t", "updated_at": "2020-01-06_16-40-00", "summary": "Renewed"},
{"id": "i8Po", "updated_at": "2020-01-08_13-16-36", "summary": "Renewed"},
{"id": "yT8h", "updated_at": "2020-01-13_18-24-05", "summary": "Deleted"},
{"id": "7uYg", "updated_at": "2020-01-18_23-37-19", "summary": "Transferred"},
]
有效的方法是什么?
最佳答案
您可以使用dict
来累积项目。
字典可以将id
存储为键,将列表项存储为值。仅当具有相同键的项目不存在时才在字典中插入项目;如果它确实比较 updated_at
值并根据需要更新字典。
def generate_new_list(my_list):
counts = {}
for d in my_list:
item_id = d['id']
if item_id in counts:
if d['updated_at'] > counts[item_id]['updated_at']:
counts[item_id] = d
else:
counts[item_id] = d
return list(counts.values())
还有一些注意事项:
- 如果您想保留原始顺序,请确保您使用的是 Python 3.7(它保证字典按插入顺序排序)或使用 OrderedDict。使用标准字典,您必须首先弹出条目,因为替换不会更改字典顺序(因此每个项目将按照其 id 首次出现的顺序输出),而ordereddict有特殊支持对于该用例(move_to_end)。
您还可以使用
dict.get
和“空对象模式”删除特殊情况:MISSING = {'updated_at': '0'} # pseudo-entry smaller than all possible def generate_new_list(my_list): counts = {} for d in my_list: if d['updated_at'] > counts.get(d['id'], MISSING): counts[d['id']] = d return list(counts.values())
- 一种非字典替代方案(尽管它在很大程度上不保持顺序)是按(id,updated_by)排序,按id分组,然后仅保留最后一个条目。我不认为 stdlib 提供了开箱即用的最后一个操作(islice 不接受负索引),因此您要么必须手动执行此操作,要么首先将子条目具体化为列表。<
关于python - 更新的词典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59852730/