python 查找多个列表的所有组合。编程新手
我刚开始学习编程,虽然我学习其他东西很快,但现在需要一些帮助。
我想在Python中找到多个列表的所有可能组合。我知道有一个叫intertool的工具,但老实说,我甚至不知道从哪里开始,怎么用它,或者怎么输入我的数据。
我想做的一个基本例子是:
Flavors Sizes Toppings Syrups
========== ======= ============= ==============
Chocolate Small Sprinkles Hot fudge
Vanilla Medium Gummy bears Caramel
Strawberry Large Oreo Strawberry
Coffee Cookie dough White chocolate
Snickers etc.
Brownies
etc.
比如说,口味和大小只能选择一个,但对于糖浆我想让他们选择三个,配料也是选择三个。我想找到所有的组合。
这难吗?我需要什么具体的代码,怎么输入我的变量?
谢谢你,真的很感激。
顺便问一下,Python能处理多少组合?普通的MacBook Pro的CPU能承受多少?
3 个回答
在编程中,有时候我们需要让程序在特定的条件下执行某些操作。这就像给程序设定了一些规则,只有当这些规则被满足时,程序才会继续运行。
比如说,你可能希望在用户输入一个数字后,程序才开始进行计算。如果用户没有输入数字,程序就会停下来,等着用户输入。这种情况就需要用到条件判断。
条件判断就像是在问一个问题:“如果这个条件成立,我就做这个事情;如果不成立,我就做另一个事情。”这样可以让程序更灵活,能够根据不同的情况做出不同的反应。
在代码中,条件判断通常用一些特定的语法来实现,比如“if”语句。通过这些语句,程序可以根据用户的输入或者其他条件来决定接下来该做什么。
总之,条件判断是编程中非常重要的一部分,它帮助程序根据不同的情况做出相应的处理,让程序更加智能和人性化。
from itertools import product, combinations, combinations_with_replacement
flavors = ["chocolate", "vanilla", "strawberry", "coffee"]
sizes = ["small", "medium", "large"]
syrups = ["hot fudge", "caramel", "strawberry", "white chocolate"]
toppings = ["sprinkles", "gummy bears", "oreos", "cookie dough", "snickers", "brownies"]
all_combos = list(
product(flavors, sizes, combinations(syrups, 3), combinations(toppings, 3))
)
from itertools import product, combinations, combinations_with_replacement
flavors = ["chocolate", "vanilla", "strawberry", "coffee"]
sizes = ["small", "medium", "large"]
toppings = ["sprinkles", "gummy bears", "oreos", "cookie dough", "snickers", "brownies"]
syrups = ["hot fudge", "caramel", "strawberry", "white chocolate"]
#
# pick a flavor and a size
for flavor,size in product(flavors, sizes):
#
# pick three toppings, but no more than one of each
for top_a, top_b, top_c in combinations(toppings, 3):
#
# pick three syrups, allowing repeats
for syr_a, syr_b, syr_c in combinations_with_replacement(syrups, 3):
#
# now do something with the result:
print(", ".join([flavor, size, top_a, top_b, top_c, syr_a, syr_b, syr_c]))
输出看起来像这样
chocolate, small, sprinkles, gummy bears, oreos, hot fudge, hot fudge, hot fudge
chocolate, small, sprinkles, gummy bears, oreos, hot fudge, hot fudge, caramel
chocolate, small, sprinkles, gummy bears, oreos, hot fudge, hot fudge, strawberry
chocolate, small, sprinkles, gummy bears, oreos, hot fudge, hot fudge, white chocolate
chocolate, small, sprinkles, gummy bears, oreos, hot fudge, caramel, caramel
chocolate, small, sprinkles, gummy bears, oreos, hot fudge, caramel, strawberry
# ... etc
# (4800 combinations in total)
补充说明:
还有一点需要注意的是,这里假设配料的顺序并不重要——也就是说,["sprinkles", "oreos", "cookie dough"]
和 ["oreos", "sprinkles", "cookie dough"]
实际上是一样的。
如果顺序很重要,你需要使用 itertools.permutations(toppings, 3)
(不允许重复使用同一种配料)或者 itertools.product(toppings, repeat=3)
(允许重复使用)。
要注意,考虑顺序会大大增加组合的数量——在这个例子中,从4800增加到92160。
我觉得你想要的东西是 product
:
示例:
import itertools
a1 = [1,2,3]
a2 = [4,5,6]
a3 = [7,8,9]
result = list(itertools.product(a1,a2,a3))
>>> print result
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)]