Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据字典列表中的特定字典键检测和删除重复项_Python_List_Dictionary_Duplicates - Fatal编程技术网

Python 根据字典列表中的特定字典键检测和删除重复项

Python 根据字典列表中的特定字典键检测和删除重复项,python,list,dictionary,duplicates,Python,List,Dictionary,Duplicates,我有一个字典列表,每个字典都是关于一篇文章的信息。有时,同一篇文章的“标题”会在词典中重复出现。我想删除这些重复的词典,以便词典列表中的每一篇文章都是唯一的,因为标题,即没有标题在词典中重复 我有 data=[{'title':'abc','source':'x','url':'abcx.com'}, {'title':'abc','source':'y','url':'abcy.com'}, {'title':'def','source':'g','url':'defg.com'}] 预期结

我有一个字典列表,每个字典都是关于一篇文章的信息。有时,同一篇文章的“标题”会在词典中重复出现。我想删除这些重复的词典,以便词典列表中的每一篇文章都是唯一的,因为
标题
,即没有标题在词典中重复

我有

data=[{'title':'abc','source':'x','url':'abcx.com'},
{'title':'abc','source':'y','url':'abcy.com'},
{'title':'def','source':'g','url':'defg.com'}]
预期结果:

data=[{'title':'abc','source':'x','url':'abcx.com'},
{'title':'def','source':'g','url':'defg.com'}]

一个快速的方法是跟踪您看到的标题:

titles_seen = set() #thank you @Mark Meyer
data = [{'title':'abc','source':'x','url':'abcx.com'},
        {'title':'abc','source':'y','url':'abcy.com'},
        {'title':'def','source':'g','url':'defg.com'}]
new_data = []
for item in data:
    if item['title'] not in titles_seen:
        new_data.append(item)
    titles_seen.add(item['title'])

正如@Mark Meyer在评论中指出的,您可以使用
title
作为字典中的键,这将消除由于标题散列而产生的重复项,或者,您可以定义一个
条目
类,然后只需使用
冻结集
(可能会有过杀):

>数据
[, ]
>>>冻结集(数据)
frozenset({,})

类条目:
定义初始化(自我、标题、来源、url):
self.title=标题
self.source=源
self.url=url
定义散列(自我):
返回散列(self.title)
定义(自身、其他):
如果存在(其他,条目):
返回self.title==其他.title
返回错误
定义(自身、其他):
返回值(非自身值)
定义报告(自我):
返回“”。格式(self.title、self.source、self.url)

但更好的方法是在添加到列表之前检查标题是否存在。

快速方法是跟踪您看到的标题:

titles_seen = set() #thank you @Mark Meyer
data = [{'title':'abc','source':'x','url':'abcx.com'},
        {'title':'abc','source':'y','url':'abcy.com'},
        {'title':'def','source':'g','url':'defg.com'}]
new_data = []
for item in data:
    if item['title'] not in titles_seen:
        new_data.append(item)
    titles_seen.add(item['title'])

正如@Mark Meyer在评论中指出的,您可以使用
title
作为字典中的键,这将消除由于标题散列而产生的重复项,或者,您可以定义一个
条目
类,然后只需使用
冻结集
(可能会有过杀):

>数据
[, ]
>>>冻结集(数据)
frozenset({,})

类条目:
定义初始化(自我、标题、来源、url):
self.title=标题
self.source=源
self.url=url
定义散列(自我):
返回散列(self.title)
定义(自身、其他):
如果存在(其他,条目):
返回self.title==其他.title
返回错误
定义(自身、其他):
返回值(非自身值)
定义报告(自我):
返回“”。格式(self.title、self.source、self.url)
但更好的方法是在添加到列表中之前检查标题是否存在。

设置两行:

tmp = set()
result = [tmp.add(i['title']) or i for i in data if i['title'] not in tmp]
两行,带set:

tmp = set()
result = [tmp.add(i['title']) or i for i in data if i['title'] not in tmp]

如果您希望
标题
是唯一的,那么
数据
可能应该是一个以
标题
为键的字典,而不是一个目录列表。如果您希望
标题
是唯一的,也许
数据
应该只是一本以
标题
为键的字典,而不是一系列的字典。你为什么不把
标题(见)
作为一个集合,这样就不是O(n²)?你为什么不把
标题(见)作为一个集合,这样就不是O(n²)?