从字典中删除重复项 [英] Removing Duplicates From Dictionary
问题描述
我有以下 Python 2.7 字典数据结构(我不控制源数据 - 来自另一个系统):
<前>{112762853378:{'dst': ['10.121.4.136'],'src': ['1.2.3.4'],'别名':['www.example.com']},112762853385:{'dst': ['10.121.4.136'],'src': ['1.2.3.4'],'别名':['www.example.com']},112760496444:{'dst': ['10.121.4.136'],'src': ['1.2.3.4']},112760496502:{'dst': ['10.122.195.34'],'src': ['4.3.2.1']},112765083670:...}字典键将始终是唯一的.Dst、src 和别名可以是重复的.所有记录都将始终具有 dst 和 src,但并非每条记录都必须具有别名,如第三条记录所示.
在示例数据中,前两条记录中的任何一条都将被删除(对我来说哪个无关紧要).第三条记录将被认为是唯一的,因为尽管 dst 和 src 相同,但缺少别名.
我的目标是删除所有 dst、src 和别名都已重复的记录 - 无论键如何.
这个菜鸟是如何做到这一点的?
另外,我对 Python 的有限理解将数据结构解释为一个字典,其中的值存储在字典中......一个字典的字典,这是正确的吗?
您可以查看字典中的每个项目(键值对),如果该值不在结果中,则将它们添加到结果字典中字典.
input_raw = {112762853378:{'dst': ['10.121.4.136'],'src': ['1.2.3.4'],'别名':['www.example.com']},112762853385:{'dst': ['10.121.4.136'],'src': ['1.2.3.4'],'别名':['www.example.com']},112760496444:{'dst': ['10.121.4.136'],'src': ['1.2.3.4']},112760496502:{'dst': ['10.122.195.34'],'src': ['4.3.2.1']}}结果 = {}对于 input_raw.items() 中的键、值:如果值不在 result.values() 中:结果[键] = 值打印结果
I have the following Python 2.7 dictionary data structure (I do not control source data - comes from another system as is):
{112762853378: {'dst': ['10.121.4.136'], 'src': ['1.2.3.4'], 'alias': ['www.example.com'] }, 112762853385: {'dst': ['10.121.4.136'], 'src': ['1.2.3.4'], 'alias': ['www.example.com'] }, 112760496444: {'dst': ['10.121.4.136'], 'src': ['1.2.3.4'] }, 112760496502: {'dst': ['10.122.195.34'], 'src': ['4.3.2.1'] }, 112765083670: ... }
The dictionary keys will always be unique. Dst, src, and alias can be duplicates. All records will always have a dst and src but not every record will necessarily have an alias as seen in the third record.
In the sample data either of the first two records would be removed (doesn't matter to me which one). The third record would be considered unique since although dst and src are the same it is missing alias.
My goal is to remove all records where the dst, src, and alias have all been duplicated - regardless of the key.
How does this rookie accomplish this?
Also, my limited understanding of Python interprets the data structure as a dictionary with the values stored in dictionaries... a dict of dicts, is this correct?
You could go though each of the items (the key value pair) in the dictionary and add them into a result dictionary if the value was not already in the result dictionary.
input_raw = {112762853378:
{'dst': ['10.121.4.136'],
'src': ['1.2.3.4'],
'alias': ['www.example.com']
},
112762853385:
{'dst': ['10.121.4.136'],
'src': ['1.2.3.4'],
'alias': ['www.example.com']
},
112760496444:
{'dst': ['10.121.4.136'],
'src': ['1.2.3.4']
},
112760496502:
{'dst': ['10.122.195.34'],
'src': ['4.3.2.1']
}
}
result = {}
for key,value in input_raw.items():
if value not in result.values():
result[key] = value
print result
这篇关于从字典中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!