打印所有组合,python [英] Printing all combinations, python

查看:57
本文介绍了打印所有组合,python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3个不同的变量,每个变量有2个可能的值,所以我总共有8个不同的组合。是否有python库函数或可用于打印所有可能组合的算法?



谢谢

解决方案

我认为您正在寻找产品

  a = [1,2] 
b = [100,200]
c = [1000,2000]

在itertools.product(a,b,c)中为p导入itertools

打印p

张照片:

 (1,100, 1000)
(1,100,2000)
(1,200,1000)
(1,200,2000)
(2,100,1000)
(2,100,2000)
(2,200,1000)
(2,200,2000)


say I have 3 different variables and each has 2 possible values, so in total I have 8 different combinations. Is there a python library function, or an algorithm that I can use to print all possible combinations?

Thanks

解决方案

I think you're looking for product:

a = [1, 2]
b = [100, 200]
c = [1000, 2000]

import itertools
for p in itertools.product(a, b, c):
    print p

prints:

(1, 100, 1000)
(1, 100, 2000)
(1, 200, 1000)
(1, 200, 2000)
(2, 100, 1000)
(2, 100, 2000)
(2, 200, 1000)
(2, 200, 2000)

这篇关于打印所有组合,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆