从另一个字典的值创建python字典?

Oge*_*gen 0 python algorithm dictionary

我试图从另一个字典的键值有效地构造一个python字典.

例如...

    dict1 = {'foo': [1, 3, 7], 'bar': [2, 4, 8]} ## note: all values in {key: value} will be unique
    ## Algorithm here...
    dict2 = {1: [3, 7], 3: [1, 7], 7: [1, 3], 2: [4, 8], 4: [2, 8], 8: [2, 4]}
Run Code Online (Sandbox Code Playgroud)

我可以通过强力方法得到这个结果,但这些词典适用于超过100000个节点的图形,所以我需要这个有效.

任何帮助将不胜感激.

And*_*ark 6

我是这样做的:

dict2 = {k: x[:i] + x[i+1:] for x in dict1.values() for i, k in enumerate(x)}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Python 2.x,则可能需要使用dict1.itervalues().