简体   繁体   English

如何从字典中以相反的顺序删除重复的键(Python)

[英]How to remove duplicated keys in reverse order from dictionary (python)

I am struggling to remove same keys from a dictionary in the case below: 在以下情况下,我正在努力从字典中删除相同的键:

{('cat', 'tiger'): 18,
 ('tiger', 'cat'): 18,
 ('chines', 'gentleman'): 7,
 ('gentleman', 'chines'): 7}

In this case, I consider reverse order ('cat', 'tiger'): 18 and ('tiger', 'cat'): 18 as same keys and try to make a new dictionary like below 在这种情况下,我将颠倒顺序('cat', 'tiger'): 18('tiger', 'cat'): 18视为相同的键,并尝试制作如下的新字典

 {('cat', 'tiger'): 18,
  ('gentleman', 'chines'): 7}

What I am trying is adapt any reverse order case, when I have a similar dictionary like {(('cage', 'cat'), 5),(('cat', 'cage'), 5)} to be merged or removed, either one. 当我有一个类似的字典要合并时,我想尝试的是适应任何逆序情况,例如{(('cage', 'cat'), 5),(('cat', 'cage'), 5)}或删除,一个。

You could do something along the following lines: 您可以按照以下方式进行操作:

d = {('cat', 'tiger'): 18,
     ('tiger', 'cat'): 18,
     ('chines', 'gentleman'): 7,
     ('gentleman', 'chines'): 7}

result = {tuple(sorted(x)): y for x, y in d.items()}
# {('cat', 'tiger'): 18, ('chines', 'gentleman'): 7}

In this dict comprehension, the last encountered value for each set of "equal" keys wins. 在此dict理解中,每组“相等”键的最后遇到的值将获胜。 With your sample data, this shouldn't matter because those keys have equla values. 对于您的样本数据,这无关紧要,因为这些键具有等效值。 Note that this also just sorts the tuples, regardless of the sorted version actually occuring in the original dict . 请注意,这也只是对元组进行排序,而不管原始dict实际出现的排序版本如何。

The problem is that you are using tuple values as keys, but tuple objects are ordered, so ('a','b') is different than ('b','a') . 问题是您使用tuple值作为键,但是tuple对象是有序的,因此('a','b')('b','a') A simple solution? 一个简单的解决方案? Use an unordered yet hashable container, ie a frozenset would work: 使用无序但可哈希的容器,即,可使用Frozenset:

>>> data = {('cat', 'tiger'): 18,
...  ('tiger', 'cat'): 18,
...  ('chines', 'gentleman'): 7,
...  ('gentleman', 'chines'): 7}
>>>
>>> data
{('cat', 'tiger'): 18, ('tiger', 'cat'): 18, ('chines', 'gentleman'): 7, ('gentleman', 'chines'): 7}
>>> {frozenset(k):v for k,v in data.items()}
{frozenset({'cat', 'tiger'}): 18, frozenset({'gentleman', 'chines'}): 7}

If you do not care about the order of the two items in the key... and the value associated with them is always the same, the easiest would probably be, assuming your starting dictionary named old_dict : 如果您不关心键中两个项目的顺序...,并且与它们关联的值始终相同,那么假设起始字典为old_dict ,最简单的方法可能是:

new_dict = dict()
for (key, value) in old_dict.items():
    new_dict[tuple(sorted(key))] = value

You just iterate through key/value pairs and add them into new_dict . 您只需遍历键/值对,然后将它们添加到new_dict Since we now sorted the key and then turned the resulting list back into tuple, we make sure than any reappearing couple just overwrites the previous entry. 由于我们现在对键进行了排序,然后将结果列表返回到元组,因此,我们确保不会再出现任何一对夫妇覆盖先前的条目。

instead of deleting items from that , you can take what you want and leave rest. 除了从中删除项目,您还可以随心所欲地休息。

data={('cat', 'tiger'): 18,
 ('tiger', 'cat'): 18,
 ('chines', 'gentleman'): 7,
 ('gentleman', 'chines'): 7}


track={}
for i,j in data.items():
    track[tuple(sorted(i))]=j

print(track)

output: 输出:

{('cat', 'tiger'): 18, ('chines', 'gentleman'): 7}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM