简体   繁体   English

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

[英]python find all combinations with multiple lists. Complete programming newbie

So I'm 100% new to programming and although I'm a very fast learner with most things, I am in need of assistance. 因此,我100%刚接触编程,尽管我在大多数事情上都学习得非常快,但仍需要帮助。

I want to find all possible combinations using multiple lists on Python. 我想使用Python上的多个列表查找所有可能的组合。 I know there is an intertool for it, but I honestly don't even know where to begin, how to use it or even how to enter my data. 我知道有一个intertool,但是老实说,我什至不知道从哪里开始,如何使用它甚至如何输入我的数据。

A basic example of what I'm trying to do: 我正在尝试做的一个基本示例:

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.

SO for flavors and sizes there can only be ONE choice, but let's say for syrups I let them pick THREE choices, and for toppings I also let them pick THREE. 因此,对于口味和大小,只能有一个选择,但是对于糖浆,我让他们选择三个选择,对于浇头,我也让他们选择三个选择。 And I want to find all combinations. 我想找到所有组合。

Is this hard to do? 这很难吗? What is the exact code I need and how exactly do I enter my variables? 我需要什么确切的代码?我该如何准确输入变量?

Thanks. 谢谢。 Much appreciated. 非常感激。

Ps- Is there a limit to how many combinations python can take? ps-python可以采用的组合数量是否有限制? How much can the cpu of an average macbook pro take? 一个普通的macbook pro的cpu可以花多少钱?

I think what you're looking for is product : 我认为您要寻找的是product

Example: 例:

import itertools 导入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)] 
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]))

and output looks like 输出看起来像

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)

Edit: 编辑:

The other thing to point out is that this presumes the order of toppings is unimportant - ie ["sprinkles", "oreos", "cookie dough"] is effectively identical to ["oreos", "sprinkles", "cookie dough"] . 要指出的另一件事是,这假定浇头的顺序并不重要-即["sprinkles", "oreos", "cookie dough"]["oreos", "sprinkles", "cookie dough"]有效相同。

If the order matters, you need to look at itertools.permutations(toppings, 3) instead (not allowing more than one of each) or itertools.product(toppings, repeat=3) (allowing multiples). 如果顺序很重要,则需要查看itertools.permutations(toppings, 3) (不允许一个以上)或itertools.product(toppings, repeat=3) (允许多个)。

Be aware that taking order into account greatly increases the number of combinations - from 4800 to 92160 in this example. 请注意,考虑顺序会大大增加组合的数量-在此示例中,组合数量从4800增加到92160。

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))
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM