【问题标题】:Combine all the keys which have same values inside a dictionary and swap keys with values and values with keys将字典中具有相同值的所有键组合在一起,并将键与值交换,将值与键交换
【发布时间】:2023-03-28 18:25:01
【问题描述】:

是否可以将字典中具有相同值的所有键组合起来,并将键与值交换,将值与键交换?

我不确定这样的事情是否可行,但这是示例。

mydict = {'./three/failures/1.log': ['UVM_ERROR: This is one error'], './one/failures/1.log': ['UVM_ERROR: This is one error'], './two/failures/1.log': ['UVM_ERROR: This is two error']}

预期输出:

{'UVM_ERROR: This is one error': ['./three/failures/1.log', ./one/failures/1.log'], 'UVM_ERROR: This is two error': ['./two/failures/1.log']}

我发现找到具有相同值的键的小提示:

>>> [k for k,v in a.items() if v == 'UVM_ERROR: This is one error']
['./one/failures/1.log', './three/failures/1.log']

在尝试其中一种解决方案后更新: 如果我的字典没有任何键的相同值,则 defaultdict 不起作用。

例如:

Dictionary :  {'./three/failures/1.log': 'UVM_ERROR: This is three error', './one/failures/1.log': 'UVM_ERROR: This is one error', './two/failures/1.log': 'UVM_ERROR: This is two error'}

输出:

defaultdict(<type 'list'>, {'U': ['./three/failures/1.log', './one/failures/1.log', './two/failures/1.log']})

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    你可以使用defaultdict:

    from collections import defaultdict
    
    mydict = {'./three/failures/1.log': 'UVM_ERROR: This is one error', './one/failures/1.log': 'UVM_ERROR: This is one error', './two/failures/1.log': 'UVM_ERROR: This is two error'}
    
    output = defaultdict(list)
    
    for k, v in mydict.items():
        output[v].append(k)
    
    print output
    

    输出:

    defaultdict(<type 'list'>, {'UVM_ERROR: This is two error': ['./two/failures/1.log'], 'UVM_ERROR: This is one error':['./three/failures/1.log', './one/failures/1.log']})
    

    defaultdict 是从dict 派生的,因此您可以像使用dict 一样使用它。如果你真的想要一个纯粹的dict,就做dict(output)

    【讨论】:

    • 根据您的解决方案通过观察更新了我的问题。
    • @npatel 它不起作用的原因是因为在您的问题中mydict 每个值都有方括号,而您的测试中没有方括号。我会更新的。这很简单:只需将v[0] 更改为v
    【解决方案2】:

    您可以使用itertools.groupbymydict 项目按值分组(doc):

    mydict = {'./three/failures/1.log': ['UVM_ERROR: This is one error'], './one/failures/1.log': ['UVM_ERROR: This is one error'], './two/failures/1.log': ['UVM_ERROR: This is two error']}
    
    from itertools import groupby
    
    out = {}
    for v, g in groupby(sorted(mydict.items(), key=lambda k: k[1]), lambda k: k[1]):
        out[v[0]] = [i[0] for i in g]
    
    print(out)
    

    打印:

    {'UVM_ERROR: This is one error': ['./three/failures/1.log', './one/failures/1.log'],
     'UVM_ERROR: This is two error': ['./two/failures/1.log']}
    

    【讨论】:

      【解决方案3】:

      这并不难。实际上,您可以在线完成所有操作!

      d = {'a':1, 'b':2}
      d0 = dict(zip(list(d.values()), list(d.keys())))
      d0
      {1: 'a', 2: 'b'}
      

      【讨论】:

      • 但是组合值相同的键呢?
      • 哦,刚才看到了,您想组合所有相同值的键...嗯...不太确定,我相信有一些方法可以做到...跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      相关资源
      最近更新 更多