翻转键和字典python中的值 [英] flip keys and values in dictionary python

查看:112
本文介绍了翻转键和字典python中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为z的字典,看起来像这样






{0:[0.28209479177387814,0.19947114020071635,0.10377687435514868,0.03377133158686996 ],
-1:[0.28209479177387814,0.19947114020071635,0.10377687435514868,07338133158686996]}。



我想翻转值和键以使



{0.28209479177387814:0,0.19947114020071635:0,0.10377687435514868:0,0.07338133158686996:0,
0.28209479177387814:-1,0.19947114020071635:-1,0.10377687435514868:-1,0.07338133158686996:-1 }



似乎有效的代码是:

  for a in z:
newdict = dict.fromkeys(z [a],a)

但它只适用于z中的其中一个键,并返回:$ / $ $ $ $ $ $ $ $ $ $ $ $ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1}



我做错了什么?

解决方案

你c在字典中没有重复键,但您可以使用元组以有意义的方式将它们组合在一起。

  from itertools import产品,链

tuples = chain.from_iterable(product(vs,[k])for k,vs in orig_dict.items())
#注意与...相同:
##tuples = []
##for k,vs in orig_dict.items():
##for [(v,k)for v in vs]:
# #tuples.append(tup)

这将产生:



pre> [(0.28209479177387814,0),(0.19947114020071635,0),
(0.10377687435514868,0),(0.07338133158686996,0),
(0.28209479177387814, -1),(0.19947114020071635,-1),
(0.10377687435514868,-1),(0.07338133158686996,-1)]

现在,如果你真的想要一些有趣的东西,你可以排序,并将它们分组在一起。

  from itertools import groupby 

groups = groupby(sorted(tuples),key = lambda kv:kv [0])

创建如下:

  [(0.07338133158686996,[(0.07338133158686996,0,
0.07338133158686996,-1]),
...]

您可以通过以下方式将它们转化为dict:

  final_dict = {k:[v [1] for v in vs] for k,vs in groups} 

哪个应该最终给出:

  {0.07338133158686996:[0,-1],
...}


I have a dictionary called z that looks like this


{0: [0.28209479177387814, 0.19947114020071635, 0.10377687435514868, 0.07338133158686996], -1: [0.28209479177387814, 0.19947114020071635, 0.10377687435514868, 0.07338133158686996]}.

I want to flip the values and keys to have

{0.28209479177387814:0, 0.19947114020071635:0, 0.10377687435514868:0, 0.07338133158686996:0, 0.28209479177387814:-1, 0.19947114020071635:-1, 0.10377687435514868:-1, 0.07338133158686996:-1}

The piece of code that seems to work is :

for a in z:
     newdict=dict.fromkeys(z[a],a)

but it only works for one of the keys in z and returns this:

{0.28209479177387814: -1, 0.07338133158686996: -1, 0.10377687435514868: -1, 0.19947114020071635: -1}

what am I doing wrong?

解决方案

You can't have duplicate keys in a dictionary, but you can pair them together using tuples in a meaningful way.

from itertools import product, chain

tuples = chain.from_iterable(product(vs, [k]) for k, vs in orig_dict.items())
# note this is identical to:
# # tuples = []
# # for k, vs in orig_dict.items():
# #     for tup in [(v, k) for v in vs]:
# #         tuples.append(tup)

That will produce:

[(0.28209479177387814, 0), (0.19947114020071635, 0),
 (0.10377687435514868, 0), (0.07338133158686996, 0),
 (0.28209479177387814, -1), (0.19947114020071635, -1),
 (0.10377687435514868, -1), (0.07338133158686996, -1)]

Now if you really wanted something interesting, you could sort that and group it together.

from itertools import groupby

groups = groupby(sorted(tuples), key=lambda kv: kv[0])

That creates something like:

[(0.07338133158686996, [(0.07338133158686996, 0,
                         0.07338133158686996, -1] ),
 ... ]

You could toss those into a dict by doing:

final_dict = {k: [v[1] for v in vs] for k, vs in groups}

Which should finally give:

{0.07338133158686996: [0, -1],
 ... }

这篇关于翻转键和字典python中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆