2个列表中所有可能的字典组合排列

Ham*_*rag 3 python dictionary permutation

假设我在python中有2个列表:

keys = [1, 2, 3, 4, 5, 6]

values = [7, 8, 9]
Run Code Online (Sandbox Code Playgroud)

我希望从这两个列表中获得所有排列

就像是:

d = [{1:7, 2:8, 3:9}, {1:8, 2:9, 3:7}, ....... ]
Run Code Online (Sandbox Code Playgroud)

我怎么能实现这一目标?

DSM*_*DSM 10

你的意思是这样的吗?

>>> import itertools
>>> keys = [1, 2, 3, 4, 5, 6]
>>> values = [7, 8, 9]
>>> d = [dict(zip(kperm, values)) for kperm in itertools.permutations(keys, len(values))]
>>> len(d)
120
>>> d[:10]
[{1: 7, 2: 8, 3: 9}, {1: 7, 2: 8, 4: 9}, {1: 7, 2: 8, 5: 9}, {1: 7, 2: 8, 6: 9}, {1: 7, 2: 9, 3: 8}, {1: 7, 3: 8, 4: 9}, {1: 7, 3: 8, 5: 9}, {1: 7, 3: 8, 6: 9}, {1: 7, 2: 9, 4: 8}, {1: 7, 3: 9, 4: 8}]
Run Code Online (Sandbox Code Playgroud)