Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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/7/python-2.7/5.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_Python 2.7 - Fatal编程技术网

Python 在字典中组合所有具有相同值的键,并用值交换键,用键交换值

Python 在字典中组合所有具有相同值的键,并用值交换键,用键交换值,python,python-2.7,Python,Python 2.7,是否可以将字典中具有相同值的所有键组合起来,用值交换键,用键交换值 我不确定这样的事情是否可能发生,但这里就是一个例子 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']} 预期产

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

我不确定这样的事情是否可能发生,但这里就是一个例子

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']})
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(,{'U':['./three/failures/1.log','./one/failures/1.log','./two/failures/1.log']))

这不太难。事实上,你可以在线完成这一切

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

这不太难。事实上,你可以在线完成这一切

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

您可以使用
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'>, {'U': ['./three/failures/1.log', './one/failures/1.log', './two/failures/1.log']})
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(,{'UVM_ERROR:This two ERROR':['./two/failures/1.log'],'UVM_ERROR:This two ERROR':['./three/failures/1.log'./one/failures/1.log'])

defaultdict
源于
dict
,因此您可以准确地使用它,就像使用
dict
一样。如果您确实想要一个纯的
dict
,只需执行
dict(输出)
即可使用
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'>, {'U': ['./three/failures/1.log', './one/failures/1.log', './two/failures/1.log']})
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(,{'UVM_ERROR:This two ERROR':['./two/failures/1.log'],'UVM_ERROR:This two ERROR':['./three/failures/1.log'./one/failures/1.log'])

defaultdict
源于
dict
,因此您可以准确地使用它,就像使用
dict
一样。如果您真的想要一个纯的
dict
,只需执行
dict(输出)
即可使用
itertools.groupby
按值对
mydict
项进行分组():

印刷品:

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

您可以使用
itertools.groupby
按值对
mydict
项进行分组():

印刷品:

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

但是组合值相同的键呢?哦,刚刚看到,你想组合所有值相同的键…嗯…不是很确定,我肯定有办法…但是组合值相同的键呢?哦,刚刚看到,你想组合所有值相同的键…嗯…不是很确定,我确信有办法做到这一点……根据您的解决方案,用观察更新了我的问题。@npatel它不起作用的原因是,在您的问题
mydict
中,每个值周围都有方括号,而测试中没有方括号。我会更新的。这很简单:只需将
v[0]
更改为
v
。根据您的解决方案用观察更新我的问题。@npatel它不起作用的原因是,在您的问题
mydict
中,每个值都有方括号,而测试中没有方括号。我会更新的。非常简单:只需将
v[0]
更改为
v