-1

所以我是 100% 的编程新手,虽然我在大多数事情上学得很快,但我需要帮助。

我想在 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.

所以对于口味和大小,只有一个选择,但假设对于糖浆,我让他们选择三种选择,而对于浇头,我也让他们选择三种。我想找到所有的组合。

这很难做到吗?我需要的确切代码是什么以及如何准确输入变量?

谢谢。非常感激。

Ps-python可以采取多少种组合有限制吗?一般macbook pro的cpu能占用多少?

4

3 回答 3

5

我认为您正在寻找的是product

例子:

导入迭代工具

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)]
于 2014-04-29T23:58:01.017 回答
1
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。

于 2014-04-30T00:15:32.273 回答
0
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))
)
于 2014-04-30T00:23:45.413 回答