Python Permutations - 获得带条件的全套结果 [英] Python Permutations - Obtaining full set of outcomes with conditions

查看:46
本文介绍了Python Permutations - 获得带条件的全套结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下具有布尔值的变量列表:

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

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

outcome_type=[True,False]

我想获得三个变量的可能结果的完整列表.我想要的条件是当 A=True 然后 B,C = False;当 B=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)

这当然只产生一组 True False 值,并且可以产生多个我不想要的 True 值.我也一直在尝试构建 if 语句,但一直在使用它.

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.

谢谢

我认为我的问题可能过于简单,所以会发布一个更复杂的例子

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]

我正在寻找 True 和 False 的所有可能排列,但想指定以下条件:

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

  1. A 的结果类型不能等于 B 的结果类型,C 和 D、E 和 F 相同

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

如果 A=True,则 C,E 必须等于 False(除了满足 B 的条件 1)

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

如果 C=True,A,E=False(除了满足 D 的条件 1)

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

如果 E=True,A,C=False(除了满足 F 的条件 1)

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

因此最终结果如下:

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

希望这是有道理的.

推荐答案

我不确定您是否涵盖了问题中的所有情况,但如果您只在每个位置的三重奏中寻找一个 True 值,您可以这样做:

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.

这篇关于Python Permutations - 获得带条件的全套结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆