如何创建一个嵌套字典,其中第一个字典的值充当第二个字典的键?

How can I create a nested dictionary where first dictionary's values act as second dictionary's keys?

对 Python3 很陌生。
目前我有两个 dictionaries.I 想从这两个字典创建一个嵌套字典,其中第一个的值作为第二个的键。

我需要这个嵌套字典来创建距离字典。

这是我目前拥有的词典。我把它们缩短了。

First_dict = {1: [513, 279, 582, 392, 297], 2: [297, 279, 513, 582, 392], 3: [581, 513, 582, 279, 392], 4: [513, 392, 582, 581, 279], 5: [392, 582, 513, 256, 279]}

Second_dict = {1: [0.6291397367847646, 0.7823399663201693, 0.8703625254182541, 1.0191414569956174, 1.0660162833624383], 2: [1.1155101452481397, 1.458507500394341, 1.7296683927743512, 1.8516961940275365, 2.0394772611422898], 3: [1.0046417248903352, 1.6138932511336632, 2.019918213435814, 2.024041235739835, 2.1045679202779155], 4: [0.8243626999510179, 1.2266312282559746, 1.2610971914206033, 1.401014162304405, 1.6046645352870748], 5: [0.9675124417465746, 1.1617482428771608, 1.3487710896578955, 1.6227371558506822, 1.6649312106185767]}

我需要将这些值配对,因为第二个字典的值是第一个字典值中的点的对应距离。

这就是我想要的。 {1: {513: 0.6291397367847646}, {279:0.7823399663201693}, {582:0.8703625254182541},{392:1.0191414569956174}, {297: 1.0660162833624383}(我写了一小段,只有嵌套字典的第一个key。

这是我尝试过的方法:

def dict_to_key(l):
        return tuple(sorted(l.items()))
({dict_to_key(second_dict[v]): k for k, v in first_dict.items()})

当我尝试这段代码时,我得到的错误是:TypeError: unhashable type: 'list'。 提前致谢。

我确定有一个 1 衬垫,但它不可读,所以这是一个 3 衬垫。

First_dict = {1: [513, 279, 582, 392, 297], 2: [297, 279, 513, 582, 392], 3: [581, 513, 582, 279, 392], 4: [513, 392, 582, 581, 279], 5: [392, 582, 513, 256, 279]}
Second_dict = {1: [0.6291397367847646, 0.7823399663201693, 0.8703625254182541, 1.0191414569956174, 1.0660162833624383], 2: [1.1155101452481397, 1.458507500394341, 1.7296683927743512, 1.8516961940275365, 2.0394772611422898], 3: [1.0046417248903352, 1.6138932511336632, 2.019918213435814, 2.024041235739835, 2.1045679202779155], 4: [0.8243626999510179, 1.2266312282559746, 1.2610971914206033, 1.401014162304405, 1.6046645352870748], 5: [0.9675124417465746, 1.1617482428771608, 1.3487710896578955, 1.6227371558506822, 1.6649312106185767]}
Third_dict = {}
for key in First_dict.keys():
    Third_dict[key] = {x:y for x,y in zip(First_dict[key],Second_dict[key])}
print(Third_dict)

输出:

{1: {513: 0.6291397367847646, 279: 0.7823399663201693, 582: 0.8703625254182541, 392: 1.0191414569956174, 297: 1.0660162833624383}, 2: {297: 1.1155101452481397, 279: 1.458507500394341, 513: 1.7296683927743512, 582: 1.8516961940275365, 392: 2.0394772611422898}, 3: {581: 1.0046417248903352, 513: 1.6138932511336632, 582: 2.019918213435814, 279: 2.024041235739835, 392: 2.1045679202779155}, 4: {513: 0.8243626999510179, 392: 1.2266312282559746, 582: 1.2610971914206033, 581: 1.401014162304405, 279: 1.6046645352870748}, 5: {392: 0.9675124417465746, 582: 1.1617482428771608, 513: 1.3487710896578955, 256: 1.6227371558506822, 279: 1.6649312106185767}}