首页 > 解决方案 > n维数组元素的Python组合

问题描述

我有一个数组列表如下

list1 = [['01', '02', '03', '04', '05', '06'], ['01', '64', '2f'], ['00', '1f', '17']]

我需要这些元素的所有可能组合,例如

010100, 01011f, 010117, 010200, 01021f, etc.

标签: pythonpython-3.xlist

解决方案


你可以使用itertools.product并得到你想要的:

import itertools

list1 = [['01', '02', '03', '04', '05', '06'], ['01', '64', '2f'], ['00', '1f', '17']]

for prd in itertools.product(*list1):
    print(''.join(prd))

输出:

010100
01011f
010117
016400
01641f
016417
012f00
...
062f00
062f1f
062f17

推荐阅读