简体   繁体   中英

Creating a combination for trees [Python]

I wanted to create an analogy to deliver my question. Let's say we have 20 pits to plant trees and we have 6 kind of trees to plant. I want to get all the possible combinations of planting this trees. I have given a combination example on the figure. Also the actual code I've tried is below but gives MemoryError. but I'm fine with just getting the first combination using it in a loop then move onto another one. I don't need to keep whole array in one 3d array

import itertools
l = [False,True]
a=list(itertools.product(l,repeat=6))
a=list(itertools.product(a,repeat=20))

• Trees' names: A,B,C,D,E,F.

• G is for no plantation.

• We have unlimited amount of every tree. So we can plant the same tree to every pit.

• We can leave every hole unplanted.

Representation of the given example:

[[A],[G],[D],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G]]

Whole combinations can be in a 3d numpy array or we can iterate over every combination via for loop.

Output example:

[[[A],[G],[D],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G]],

[[A],[G],[D],[G],[G],[G],[G],[G],[G],[G],[G],[G],[G],[A],[G],[G],[G],[G],[G],[G]],

[[A],[A],[A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A], [A]]]

在此处输入图像描述

A simpler approach is to model your problem with a list of cases: A -> G and then use itertools.product() . It may be possible that list(itertools.product()) still gives you a memory error. In that case, just use next() to pull only the subsequent combination:

import itertools
import string

cases = list(string.ascii_uppercase[:7])
gen = itertools.product(cases, repeat=20)


for i in range(10): 
    print(next(gen))
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'C')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'D')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'E')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'F')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'G')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'A')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B')
# ('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'C')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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