我有字典列表,我想从这个列表中删除重复的内容。怎么做?
a = [
{'dtstart': '2014-09-10T08:00:00',
'end': datetime.datetime(2014, 9, 10, 9, 0),
'location': 'Brady Auditorium, B-131',
'partial_date': datetime.date(2014, 9, 10),
'photo': 'http://tools.medicine.yale.edu/portal/stream?id=01a331e2-42be-4622-b072-0c42b55b436e&w=540&h=700',
'start': datetime.datetime(2014, 9, 10, 8, 0),
'stream': '01a331e2-42be-4622-b072-0c42b55b436e',
'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"',
'uid': '2d671415-c666-498f-a401-01652a08e4b3'},
{'dtstart': '2014-09-10T08:00:00',
'end': datetime.datetime(2014, 9, 10, 9, 0),
'location': 'Brady Auditorium, B-131',
'partial_date': datetime.date(2014, 9, 10),
'photo': 'http://tools.medicine.yale.edu/portal/stream?id=ccf667b2-b5a0-464f-8797-66eb36b0bf6c&w=540&h=700',
'start': datetime.datetime(2014, 9, 10, 8, 0),
'stream': 'ccf667b2-b5a0-464f-8797-66eb36b0bf6c',
'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"',
'uid': '2d671415-c666-498f-a401-01652a08e4b3'}
]我试过的是
>>> [dict(t) for t in set([tuple(d.items()) for d in a])]但仍然返回重复的元素。
发布于 2014-09-09 12:51:11
只需尝试以下代码:
{document['uid']: document for document in a}.values()对于每一个uuid,您将得到最新的文档。如果您正在寻找第一个条目,请尝试如下:
{document['uid']: document for document in a[::-1]}.values()发布于 2014-09-09 12:50:56
使用字典理解来创建一个以uid作为键,每个字典作为值的字典。然后提取值以返回由uid键指定的唯一字典列表。
>>> a=[{'end': datetime.datetime(2014, 9, 10, 9, 0), 'uid': '2d671415-c666-498f-a401-01652a08e4b3', 'stream': '01a331e2-42be-4622-b072-0c42b55b436e', 'photo': 'http://tools.medicine.yale.edu/portal/stream?id=01a331e2-42be-4622-b072-0c42b55b436e&w=540&h=700', 'partial_date': datetime.date(2014, 9, 10), 'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"', 'start': datetime.datetime(2014, 9, 10, 8, 0), 'location': 'Brady Auditorium, B-131', 'dtstart': '2014-09-10T08:00:00'}, {'end': datetime.datetime(2014, 9, 10, 9, 0), 'uid': '2d671415-c666-498f-a401-01652a08e4b3', 'stream': 'ccf667b2-b5a0-464f-8797-66eb36b0bf6c', 'photo': 'http://tools.medicine.yale.edu/portal/stream?id=ccf667b2-b5a0-464f-8797-66eb36b0bf6c&w=540&h=700', 'partial_date': datetime.date(2014, 9, 10), 'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"', 'start': datetime.datetime(2014, 9, 10, 8, 0), 'location': 'Brady Auditorium, B-131', 'dtstart': '2014-09-10T08:00:00'}]
>>> {d['uid']: d for d in a}.values()
[{'dtstart': '2014-09-10T08:00:00',
'end': datetime.datetime(2014, 9, 10, 9, 0),
'location': 'Brady Auditorium, B-131',
'partial_date': datetime.date(2014, 9, 10),
'photo': 'http://tools.medicine.yale.edu/portal/stream?id=ccf667b2-b5a0-464f-8797-66eb36b0bf6c&w=540&h=700',
'start': datetime.datetime(2014, 9, 10, 8, 0),
'stream': 'ccf667b2-b5a0-464f-8797-66eb36b0bf6c',
'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"',
'uid': '2d671415-c666-498f-a401-01652a08e4b3'}]发布于 2014-09-09 12:50:33
尝试将uids附加到临时列表中,并使用当前字典进行验证。
import datetime
a=[{'end': datetime.datetime(2014, 9, 10, 9, 0), 'uid': '2d671415-c666-498f-a401-01652a08e4b3', 'stream': '01a331e2-42be-4622-b072-0c42b55b436e', 'photo': 'http://tools.medicine.yale.edu/portal/stream?id=01a331e2-42be-4622-b072-0c42b55b436e&w=540&h=700', 'partial_date': datetime.date(2014, 9, 10), 'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"', 'start': datetime.datetime(2014, 9, 10, 8, 0), 'location': 'Brady Auditorium, B-131', 'dtstart': '2014-09-10T08:00:00'},
{'end': datetime.datetime(2014, 9, 10, 9, 0), 'uid': '2d671415-c666-498f-a401-01652a08e4b3', 'stream': 'ccf667b2-b5a0-464f-8797-66eb36b0bf6c', 'photo': 'http://tools.medicine.yale.edu/portal/stream?id=ccf667b2-b5a0-464f-8797-66eb36b0bf6c&w=540&h=700', 'partial_date': datetime.date(2014, 9, 10), 'summary': 'Clinical Neuroscience Grand Rounds: "The Mechanism of Impaired Consciousness of Absence Seizures"', 'start': datetime.datetime(2014, 9, 10, 8, 0), 'location': 'Brady Auditorium, B-131', 'dtstart': '2014-09-10T08:00:00'}]
uuids = set() # temperary set holds UID
final=[]
for i in a:
if i['uid'] not in uuids:
final.append(i)
uuids.add(i['uid'])
print finalhttps://stackoverflow.com/questions/25745002
复制相似问题