Will Beauchamp
Will Beauchamp

Reputation: 579

Return all possible combinations when selecting a single item from multiple bins, with an IF condition

If I need all combinations possible when selecting an element from N different bins I can do:

all_possible_cominations = [selection for selection in itertools.product(bin1,bin2,bin3...)]

But in this instance I have a problem where the element chosen in bin1 changes which bins are relevant for the rest of the selections.

For example:

bin1 = [1,2], bin2 = [3,4], bin3 = [5,6]

if we select 1 from bin1 then we select 0 items from bin2 and 1 item from bin3, if we select 2 from bin1 then we select 1 item from bin2 and 0 items from bin3.

so the exhaustive list of combinations would be [1,,5],[1,,6],[2,3,],[2,4,]

Any help?

Upvotes: 0

Views: 59

Answers (1)

Ohumeronen
Ohumeronen

Reputation: 2086

I am not sure if I got your question right. Would this help you?

import itertools

bin1 = [1,2]
bin2 = [3,4]
bin3 = [5,6]

print [[bin1[0],y] for y in bin3] + [[bin1[1],y] for y in bin2]

Upvotes: 2

Related Questions