简体   繁体   English

在Python中获取变量/参数的所有可能组合,但始终包括所有变量

[英]Get all possible combinations of variables/arguments in Python but always including all variables

I'm wanting to write a Python script that provides all possible combinations of some variables and arguments but to always include all variables in the results. 我想编写一个Python脚本,该脚本提供某些变量和参数的所有可能组合,但始终将所有变量包括在结果中。

I've tried a few attempts but the closest I can get is literally all combinations. 我尝试了一些尝试,但实际上我能得到的最接近的是所有组合。 For example, let's say a, b, c, d and e can either be true or false. 例如,假设a,b,c,d和e可以为true或false。 I want all possible combinations but importantly I'm trying to include all of those variables each time. 我想要所有可能的组合,但重要的是,我每次都试图包含所有这些变量。 So the following two examples are valid among the results: a:true, b:false, c:false, d:false, e:false a:true, b:true, c:false, d:false, e:false (note b is different) 因此,以下两个示例在结果中有效:a:true,b:false,c:false,d:false,e:false a:true,b:true,c:false,d:false,e:false(注意b是不同的)

But the following is NOT what I'm looking for: a:true, c:false 但是以下不是我想要的:a:true,c:false

I want all possible combinations but always including ae. 我想要所有可能的组合,但始终包括ae。

There is an answer already provided by Martijn Pieters, which is basically what I want minus the requirement that all variables are represented ( Find all possible combinations of arguments in Python ). Martijn Pieters已经提供了一个答案,这基本上是我想要的,减去了表示所有变量的要求( 在Python中找到参数的所有可能组合 )。 Here is the code by Pieters: 这是Pieters的代码:

from itertools import product

for combo in product((None, True, False), repeat=3):
    arguments = {k: v for k, v in zip('abc', combo) if v is not None}
    print arguments

>>> from itertools import product
>>> for combo in product((None, True, False), repeat=3):
...     arguments = {k: v for k, v in zip('abc', combo) if v is not None}
...     print arguments
... 
{}
{'c': True}
{'c': False}
{'b': True}
{'c': True, 'b': True}
{'c': False, 'b': True}
{'b': False}
{'c': True, 'b': False}
{'c': False, 'b': False}
{'a': True}
{'a': True, 'c': True}
{'a': True, 'c': False}
{'a': True, 'b': True}
{'a': True, 'c': True, 'b': True}
{'a': True, 'c': False, 'b': True}
{'a': True, 'b': False}
{'a': True, 'c': True, 'b': False}
{'a': True, 'c': False, 'b': False}
{'a': False}
{'a': False, 'c': True}
{'a': False, 'c': False}
{'a': False, 'b': True}
{'a': False, 'c': True, 'b': True}
{'a': False, 'c': False, 'b': True}
{'a': False, 'b': False}
{'a': False, 'c': True, 'b': False}
{'a': False, 'c': False, 'b': False}
'''

Ignoring the None option this is almost exactly what I've been trying to do but in this example I wouldn't want to just return that a is false. 忽略None选项几乎就是我一直在尝试的操作,但是在此示例中,我不想仅仅返回a为假。 I would want it to return far fewer combinations because a, b, and c should always be included. 我希望它返回的组合要少得多,因为应始终包含a,b和c。 It's just the combination of whether they're true or false I want to get from this. 我想从中得出它们是真还是假的组合。

What would be the best way to modify this example to achieve the correct results? 修改此示例以获取正确结果的最佳方法是什么? Or do you recommend a completely different approach? 还是您建议一种完全不同的方法? Many thanks. 非常感谢。

Since this is merely a binary counter, you don't need itertools: 由于这只是一个二进制计数器,因此不需要itertools:

params = "abcd"
N      = len(params)
combos = [{params[p]:n&(1<<p)>0 for p in range(N)} for n in range(1<<N)] 

[{'a': False, 'b': False, 'c': False, 'd': False}, 
 {'a': True,  'b': False, 'c': False, 'd': False}, 
 {'a': False, 'b': True,  'c': False, 'd': False}, 
 {'a': True,  'b': True,  'c': False, 'd': False}, 
 {'a': False, 'b': False, 'c': True,  'd': False}, 
 {'a': True,  'b': False, 'c': True,  'd': False}, 
 {'a': False, 'b': True,  'c': True,  'd': False}, 
 {'a': True,  'b': True,  'c': True,  'd': False}, 
 {'a': False, 'b': False, 'c': False, 'd': True}, 
 {'a': True,  'b': False, 'c': False, 'd': True}, 
 {'a': False, 'b': True,  'c': False, 'd': True}, 
 {'a': True,  'b': True,  'c': False, 'd': True}, 
 {'a': False, 'b': False, 'c': True,  'd': True}, 
 {'a': True,  'b': False, 'c': True,  'd': True}, 
 {'a': False, 'b': True,  'c': True,  'd': True}, 
 {'a': True,  'b': True,  'c': True,  'd': True}]

I have to admit though that using product makes it a bit easier to read (but that may be a matter of opinion): 我必须承认,尽管使用产品可以使阅读起来更容易(但这可能是一个观点问题):

params = "abcd"
N      = len(params)
combos = [{p:v for p,v in zip(params,c)} for c in product(*[(False,True)]*N)]

Simply remove None from the possible values and repeat by the number of your variable count: 只需从可能的值中删除“ None ,然后重复变量计数的数字即可:

from itertools import product

vars = 'abcde'
for combo in product((True, False), repeat=len(vars)):
    arguments = {k: v for k, v in zip(vars, combo)}
    print( arguments )

Note that if you keep repeat=3 , you will only get results for ac. 请注意,如果保持repeat=3 ,则只会得到ac的结果。

As @Colin mentioned, you may remove if v is not None , as the value can never be None in this case. 如@Colin所述, if v is not Noneif v is not None可以删除,因为在这种情况下该值永远不能为None

Output: 输出:

{'e': True, 'd': True, 'b': True, 'c': True, 'a': True}
{'e': False, 'd': True, 'b': True, 'c': True, 'a': True}
{'e': True, 'd': False, 'b': True, 'c': True, 'a': True}
{'e': False, 'd': False, 'b': True, 'c': True, 'a': True}
{'e': True, 'd': True, 'b': True, 'c': False, 'a': True}
{'e': False, 'd': True, 'b': True, 'c': False, 'a': True}
{'e': True, 'd': False, 'b': True, 'c': False, 'a': True}
{'e': False, 'd': False, 'b': True, 'c': False, 'a': True}
{'e': True, 'd': True, 'b': False, 'c': True, 'a': True}
{'e': False, 'd': True, 'b': False, 'c': True, 'a': True}
{'e': True, 'd': False, 'b': False, 'c': True, 'a': True}
{'e': False, 'd': False, 'b': False, 'c': True, 'a': True}
{'e': True, 'd': True, 'b': False, 'c': False, 'a': True}
{'e': False, 'd': True, 'b': False, 'c': False, 'a': True}
{'e': True, 'd': False, 'b': False, 'c': False, 'a': True}
{'e': False, 'd': False, 'b': False, 'c': False, 'a': True}
{'e': True, 'd': True, 'b': True, 'c': True, 'a': False}
{'e': False, 'd': True, 'b': True, 'c': True, 'a': False}
{'e': True, 'd': False, 'b': True, 'c': True, 'a': False}
{'e': False, 'd': False, 'b': True, 'c': True, 'a': False}
{'e': True, 'd': True, 'b': True, 'c': False, 'a': False}
{'e': False, 'd': True, 'b': True, 'c': False, 'a': False}
{'e': True, 'd': False, 'b': True, 'c': False, 'a': False}
{'e': False, 'd': False, 'b': True, 'c': False, 'a': False}
{'e': True, 'd': True, 'b': False, 'c': True, 'a': False}
{'e': False, 'd': True, 'b': False, 'c': True, 'a': False}
{'e': True, 'd': False, 'b': False, 'c': True, 'a': False}
{'e': False, 'd': False, 'b': False, 'c': True, 'a': False}
{'e': True, 'd': True, 'b': False, 'c': False, 'a': False}
{'e': False, 'd': True, 'b': False, 'c': False, 'a': False}
{'e': True, 'd': False, 'b': False, 'c': False, 'a': False}
{'e': False, 'd': False, 'b': False, 'c': False, 'a': False}

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

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