zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    Fault-Tolerant Virtual Machine 论文笔记
    Google File System 论文笔记
    Amazon Aurora 论文笔记
    MATLAB入门学习(二):分支语句和编程设计
    MATLAB入门学习(一):基础准备
    矩阵连乘问题
    合并排序 java
    生产者与消费者 代码实现 java
    图的封装(C++)
    二叉树的封装
  • 原文地址:https://www.cnblogs.com/BH8ANK/p/9044619.html
Copyright © 2011-2022 走看看