1

I have the following list of variables that take on a Boolean value:

outcome_value=['A','B','C']
outcome_type=[True,False]

I want to obtain the full list for possible outcomes for the three variables. The conditions I want are when A=True then B,C = False; when B=True the others equal False and so on. in other words:

outcomes_all = [(True, False, False), (False, True, False), (False, False, True)]

What code could I use to obtain the above? Please note this is a simplification of what I am looking to do I am looking to get code I could use to extend to a more complex list of outcomes.

Only thing i can think of so far is as follows

import random
        for o in outcome_value:
            p=random.choice(outcome_type)
            print(p)

which of course only produces one set of True False values and can produce more than one True value which I don't want. I have been trying to build out if statements as well but keep running down blind alleys with it.

Does anyone have any ideas on how to do this? Haven't seen any threads with a similar question yet.

Thanks


Edit: thinkI may have oversimplified my questions so will post a more complex example

outcome_value=['A','B','C','D','E','F']
outcome_type=[True,False]

I'm looking for all possible permutations of True and False but want to specify conditions such as:

  1. outcome_type of A must not equal outcome_type of B, same for C and D, E and F

  2. if A=True, then C,E must equal False (in addition to condition 1 for B being met also)

  3. if C=True, A,E=False (in addition to condition 1 for D being met also)

  4. if E=True, A,C=False (in addition to condition 1 for F being met also)

Therefore final outcome would be like follows:

[(True, False, False, True ,False ,True), (False, True, False, False, False ,True), (False, False, True, True, True, False)]

Hope that makes sense.

3 Answers 3

2

I'm not sure you're covering all cases in your question but if you're only looking for one True value in the trio for each position, you can do this:

outcome_value = ['A','B','C']
outcome_type  = [True,False]

outcomes_all = [ tuple(outcome_type[t!=p] for t in outcome_value) for p in outcome_value ]

# [(True, False, False), (False, True, False), (False, False, True)]

This is simple enough but I suspect there's more to it than that. Given that you requested the full list, random should not come into play.

Sign up to request clarification or add additional context in comments.

4 Comments

This seems to be a sensible approach. However, I got different results here:?[(False, True, True), (True, False, True), (True, True, False)]
Fixed, I didn't realize the True and False were inverted in outcome_type
thanks, I'm getting two True values when I run the above though? [(False, True, True), (True, False, True), (True, True, False)]...dont think this would work anyway since if I want to extend the list (ie add D, E, F etc) there will be more than one True and False value for each. Therefore am more looking for specific code that recognizes the values so that if say A= True then set B,C to False, but others could still be either true or false
did you copy it after the fix ? (outcome_type[t!=p]) I tried is again and it works as indicated
2

You can get the list of values by filtering out the Cartesian product:

from itertools import product
outcomes_all = list(filter(
    lambda x: sum(x) == 1,
    product(outcome_type, repeat=3)
))

That would be the output:

>>> outcomes_all
[(True, False, False), (False, True, False), (False, False, True)]

Comments

0

You mean like this?

import itertools
outcome_value=['A','B','C']

outcome_value = False,False,True

combin = itertools.permutations(outcome_value,3)

print(list(combin))

This is the output

[(False, False, True), (False, True, False), (False, False, True), (False, True, False), (True, False, False), (True, False, False)]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.