Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python查找具有多个列表的所有组合。完全编程新手_Python_Variables_Loops_Combinations_Factors - Fatal编程技术网

python查找具有多个列表的所有组合。完全编程新手

python查找具有多个列表的所有组合。完全编程新手,python,variables,loops,combinations,factors,Python,Variables,Loops,Combinations,Factors,因此,我对编程是100%的新手,尽管我在大多数方面都学得很快,但我需要帮助 我想在Python上使用多个列表查找所有可能的组合。我知道它有一个intertool,但老实说,我甚至不知道从哪里开始,如何使用它,甚至不知道如何输入我的数据 我正在尝试做的一个基本示例: Flavors Sizes Toppings Syrups ========== ======= ============= ============== Chocolate

因此,我对编程是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.
所以对于口味和大小只能有一种选择,但假设糖浆我让他们选择三种,而对于配料我也让他们选择三种。我想找到所有的组合

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

谢谢。非常感谢


附言-python可以接受的组合数量有限制吗?一台普通macbook pro的cpu能占用多少?

我想你要找的是
产品

进口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)]
输出看起来像

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)
编辑:

需要指出的另一点是,这假定浇头的顺序并不重要-即
[“洒”、“奥利奥”、“饼干面团”]
实际上与
[“奥利奥”、“洒”、“饼干面团”]
完全相同

如果顺序很重要,您需要查看
itertools.permutations(toppings,3)
(每个排列不允许超过一个)或
itertools.product(toppings,repeat=3)
(允许倍数)


请注意,考虑顺序会大大增加组合的数量——在本例中从4800个增加到92160个。

浇头和糖浆——可以重复吗,即3次喷洒一个选项?-1“我需要的确切代码是什么?”并不是问题。这不会从
a1
a2
返回1个项目,根据要求,从
a3
中选择3个。
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"]
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))
)