首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【python 3.6】使用itertools.product进行排列组合

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

作者头像
一枚hammer
发布于 2021-01-28 20:32:22
发布于 2021-01-28 20:32:22
1.4K0
举报
文章被收录于专栏:云技术分享云技术分享
代码语言:javascript
复制
#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)

输出如下:

代码语言:javascript
复制
[('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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档