我有一个 dics 列表:
data = {}
data['key'] = pointer_key
data['timestamp'] = timestamp
data['action'] = action
data['type'] = type
data['id'] = id
list = [data1, data2, data3, ... ]
Run Code Online (Sandbox Code Playgroud)
如何确保对于列表中的每个数据项,每个“键”只存在一个这样的元素?如果有如下所示的两个键,则最近的时间戳将获胜:
list = [{'key':1,'timestamp':1234567890,'action':'like','type':'photo',id:245},
{'key':2,'timestamp':2345678901,'action':'like','type':'photo',id:252},
{'key':1,'timestamp':3456789012,'action':'like','type':'photo',id:212}]
unique(list)
list = [{'key':2,'timestamp':2345678901,'action':'like','type':'photo',id:252},
{'key':1,'timestamp':3456789012,'action':'like','type':'photo',id:212}]
Run Code Online (Sandbox Code Playgroud)
谢谢。
小智 5
这是我的解决方案:
def uniq(list_dicts):
return [dict(p) for p in set(tuple(i.items())
for i in list_dicts)]
Run Code Online (Sandbox Code Playgroud)
希望它会帮助某人。
我需要这个,但不喜欢这里的任何答案。所以我做了这个简单且高性能的版本。
def list_of_seq_unique_by_key(seq, key):
seen = set()
seen_add = seen.add
return [x for x in seq if x[key] not in seen and not seen_add(x[key])]
# Usage
# If you want most recent timestamp to win, just sort by timestamp first
list = sorted(list, key=lambda k: k['timestamp'], reverse=True)
# Remove everything with a duplicate value for key 'key'
list = list_of_seq_unique_by_key(list, 'key')
Run Code Online (Sandbox Code Playgroud)