【python 3.6】使用itertools.product进行排列组合

#python 3.6
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'BH8ANK'

import itertools


color = [
    'red',
    'green',
    'blue',
    'white'
    ]

target = [
    'bike',
    'pencil',
    'desk',
    'gun',
    'car'
]

data_source = itertools.product(color,target)
data_source_list = [it for it in data_source]
print(data_source_list)

输出如下:

[('red', 'bike'), ('red', 'pencil'), ('red', 'desk'), ('red', 'gun'), ('red', 'car'), ('green', 'bike'), ('green', 'pencil'), ('green', 'desk'), ('green', 'gun'), ('green', 'car'), ('blue', 'bike'), ('blue', 'pencil'), ('blue', 'desk'), ('blue', 'gun'), ('blue', 'car'), ('white', 'bike'), ('white', 'pencil'), ('white', 'desk'), ('white', 'gun'), ('white', 'car')]

即,itertools.product(list1,list2),将list1和list2中的元素依次排列组合,返回一个新的list

猜你喜欢

转载自www.cnblogs.com/BH8ANK/p/9044619.html