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

  • 相关阅读:
    SonarQube 之 权限模板配置
    window jenkins + sonarqube + sonar-scanner 最佳实践
    到某个指定的页面不可以后退
    php里面判断是pc还是手机
    爱奇艺视频引入
    给电脑安装字体
    应用宝下线
    ps 如何的复制想要的东西,以及修改
    实现倒计时的效果(3秒倒计时)
    数据库里的数据被删除,依然想按照顺序往下递增的解决办法
  • 原文地址:https://www.cnblogs.com/BH8ANK/p/9044619.html
Copyright © 2011-2022 走看看