【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'
]

type = [
    'big',
    'small'
]

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

 

 

输出如下:

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

 

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

 

转载于:https://www.cnblogs.com/BH8ANK/p/9044619.html

你可能感兴趣的:(python)