python中两个列表的排列映射

Shi*_*oel 3 python dictionary list permutation

如何在python中创建两个列表的排列映射?

例如,我有两个列表[1,2,3]['A','B','C']

然后我的代码应该生成一个包含6个词典的列表

[ {1:'A',2:'B',3:'C'},
  {1:'A',2:'C',3:'B'},
  {1:'B',2:'A',3:'C'},
  {1:'B',2:'C',3:'A'},
  {1:'C',2:'A',3:'B'},
  {1:'C',2:'B',3:'A'} ]
Run Code Online (Sandbox Code Playgroud)

wim*_*wim 11

使用zipitertools.permutations列表理解:

>>> from itertools import permutations
>>> L1 = [1,2,3]
>>> L2 = ['A','B','C']
>>> [dict(zip(L1, p)) for p in permutations(L2)]
[{1: 'A', 2: 'B', 3: 'C'},
 {1: 'A', 2: 'C', 3: 'B'},
 {1: 'B', 2: 'A', 3: 'C'},
 {1: 'B', 2: 'C', 3: 'A'},
 {1: 'C', 2: 'A', 3: 'B'},
 {1: 'C', 2: 'B', 3: 'A'}]
Run Code Online (Sandbox Code Playgroud)