生成可能配置的列表 [英] Generating list of possible configurations

查看:67
本文介绍了生成可能配置的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello pythonistas。


我几乎都是编程和Python的新手。我有一个任务

,其中包括为我正在测试的软件的每个可能的

首选项设置组合编写一个测试脚本。我认为这个

是一个脚本可以很容易做到的事情,给出了各种各样的可能性。


我开始创建所有设置的字典,其中每个键

都有一个值,该值是该设置的可能值列表。

大多数设置都是简单的布尔值(设置打开或关闭),其中一些

是带有多个值的下拉菜单。例如:


settings = {

''setting_a'':( True,False),

''setting_b' ':(正确,错误),

''setting_c'':( 1,2,3,4),

}


在此之后,我尝试找出一个可以生成不同可能配置的函数,但是我无法完全包裹我的脑袋

...是否有适合此类任务的一般模式/结构

?关于如何解决问题的任何指示

解决这样的问题将非常感激。我不需要使用Python,但我认为这可能是一个很好的学习练习......: - ) >

亲切的问候,

// Emil

解决方案

7月2日,4: 53 * pm,bjorklund.e ... @ gmail.com

< bjorklund.e ... @ gmail.comwrote:


Hello pythonistas。


我几乎都是编程和Python的新手。我有一个任务

,其中包括为我正在测试的软件的每个可能的

首选项设置组合编写一个测试脚本。我认为这个

是一个脚本可以很容易做到的事情,给出了各种各样的可能性。


我开始创建所有设置的字典,其中每个键

都有一个值,该值是该设置的可能值列表。

大多数设置都是简单的布尔值(设置打开或关闭),其中一些

是带有多个值的下拉菜单。例如:


settings = {

* *''setting_a'':(正确,错误),

* * ''setting_b'':(正确,错误),

* *''setting_c'':( 1,2,3,4),


}


在此之后,我尝试找出一个可以生成不同可能配置的函数,但是我无法完全包裹我的脑袋。 >
左右...



基本上,对于a的每个设置,你必须做每一个可能的b,

和对于每个a,b设置你必须做每个可能的c等。


是否有适合的一般模式/结构

for这类任务?



查询Cartesian产品。


关于如何进行的任何指示

解决这样的问题将非常感激。



for [True,False]:

for b in [True,False]:

对于[1,2,3,4]中的c:

打印''组合设置:'',a,''\ t'',b,''\ t'', c


组合设置:True True 1

组合设置:True True 2

组合设置:True True 3

组合设置:True True 4

组合设置:True False 1

组合设置:True False 2

组合设置:真假3

组合设置:正确4

组合设置:假真1

组合设置:假真2

组合设置:False True 3

组合设置:False True 4

组合设置:False False 1

组合设置:False False 2

组合设置:False False 3

组合设置:False False 4


It 不是我需要使用Python才能获得它,但我认为这可能是一个很好的学习练习...... :-)



您可能也有兴趣


替换排列

无替换排列

组合与替换

组合无替换


>

亲切的问候,

// Emil





Mensanator写道:


7月2日下午4:53,bjorklund.e ... @ gmail.com

< bjorklund.e ... @ gmail.comwrote :


>在此之后,我尝试找出一个可以生成不同可能配置的函数,但我不能完全包裹我的脑袋......


LookupCartesian Product。


>关于如何进行的任何指示



for [True,False]:

for b in [True,False]:

对于[1,2,3,4]中的c:

打印''组合设置:'',a,''\ t'',b,''\ t'', c



这已被添加到itertools至少为2.6 / 3.0

< blockquote class =post_quotes>
>>导入itertools,因为它对于prod in it.product((True,False),(True,False),(1,2,3,4) )):



print(prod)#或运行测试


(True,没错,1)

(真,真,2)

(真,真,3)

(真,真,4)

(真,假,1)

(真,假,2)

(真,假,3)

(真,假,4)

(假,真,1)

(假,真,2)

(假,是的,3)

(假,真,4)

(假,假,1)

(假,假, 2)

(假,假,3)

(假,假,4)


序列序列可以,当然,是一个变量:


>> options =((True,False ),(真,假),(1,2,3,4))
对于prod in it.product(* options):print(prod)



做同样的事情。因此,您可以在不更改

测试运行器的情况下更改选项。


tjr


Terry Reedyaécrit:


>


Mensanator写道:


(剪辑)


> LookupCartesian Product。



(snip)


> for a [True,False]中的a:
对于[真,假]中的b:
对于[1,2,3,4]中的c:
打印''组合设置:'' ,a,''\t'',b,''\ t'',c



这已被添加到itertools至少为2.6 / 3.0



太棒了!


Hello pythonistas.

I''m a newbie to pretty much both programming and Python. I have a task
that involves writing a test script for every possible combination of
preference settings for a software I''m testing. I figured that this
was something that a script could probably do pretty easily, given all
the various possibilites.

I started creating a dictionary of all the settings, where each key
has a value that is a list of the possible values for that setting.
Most of the settings are simple booleans (setting is on or off), some
of them are drop-downs with several values. For example:

settings = {
''setting_a'': (True, False),
''setting_b'': (True, False),
''setting_c'': (1, 2, 3, 4),
}

After this I tried figuring out a function that would generate the
different possible configurations, but I couldn''t quite wrap my head
around it... Are there any general patterns/structures that are suited
for this type of task? Any pointers as to how one would go about
solving something like this would be greatly appreciated. It''s not
that I really need to use Python for it, but I thought it could be a
good learning excercise... :-)

Kind regards,
//Emil

解决方案

On Jul 2, 4:53*pm, "bjorklund.e...@gmail.com"
<bjorklund.e...@gmail.comwrote:

Hello pythonistas.

I''m a newbie to pretty much both programming and Python. I have a task
that involves writing a test script for every possible combination of
preference settings for a software I''m testing. I figured that this
was something that a script could probably do pretty easily, given all
the various possibilites.

I started creating a dictionary of all the settings, where each key
has a value that is a list of the possible values for that setting.
Most of the settings are simple booleans (setting is on or off), some
of them are drop-downs with several values. For example:

settings = {
* * ''setting_a'': (True, False),
* * ''setting_b'': (True, False),
* * ''setting_c'': (1, 2, 3, 4),

}

After this I tried figuring out a function that would generate the
different possible configurations, but I couldn''t quite wrap my head
around it...

Basically, for each setting of a, you must do each possible b,
and for each a,b setting you must do each possible c, etc.

Are there any general patterns/structures that are suited
for this type of task?

Lookup "Cartesian Product".

Any pointers as to how one would go about
solving something like this would be greatly appreciated.

for a in [True,False]:
for b in [True,False]:
for c in [1,2,3,4]:
print ''combined settings:'',a,''\t'',b,''\t'',c

combined settings: True True 1
combined settings: True True 2
combined settings: True True 3
combined settings: True True 4
combined settings: True False 1
combined settings: True False 2
combined settings: True False 3
combined settings: True False 4
combined settings: False True 1
combined settings: False True 2
combined settings: False True 3
combined settings: False True 4
combined settings: False False 1
combined settings: False False 2
combined settings: False False 3
combined settings: False False 4

It''s not
that I really need to use Python for it, but I thought it could be a
good learning excercise... :-)

You may, then, also be interested in

Permutations with Replacement
Permutations without Replacement
Combinations with Replacement
Combinations without Replacement

>
Kind regards,
//Emil




Mensanator wrote:

On Jul 2, 4:53 pm, "bjorklund.e...@gmail.com"
<bjorklund.e...@gmail.comwrote:

>After this I tried figuring out a function that would generate the
different possible configurations, but I couldn''t quite wrap my head
around it...

Lookup "Cartesian Product".

>Any pointers as to how one would go about
solving something like this would be greatly appreciated.


for a in [True,False]:
for b in [True,False]:
for c in [1,2,3,4]:
print ''combined settings:'',a,''\t'',b,''\t'',c

This has been added to itertools at least for 2.6/3.0

>>import itertools as it
for prod in it.product((True,False), (True,False), (1,2,3,4)):

print(prod) # or run test

(True, True, 1)
(True, True, 2)
(True, True, 3)
(True, True, 4)
(True, False, 1)
(True, False, 2)
(True, False, 3)
(True, False, 4)
(False, True, 1)
(False, True, 2)
(False, True, 3)
(False, True, 4)
(False, False, 1)
(False, False, 2)
(False, False, 3)
(False, False, 4)

The sequences of sequences can, of course, be a variable:

>>options = ((True,False), (True,False), (1,2,3,4))
for prod in it.product(*options): print(prod)

does the same thing. So you can change ''options'' without changing the
test runner.

tjr


Terry Reedy a écrit :

>

Mensanator wrote:

(snip)

>Lookup "Cartesian Product".

(snip)

>for a in [True,False]:
for b in [True,False]:
for c in [1,2,3,4]:
print ''combined settings:'',a,''\t'',b,''\t'',c


This has been added to itertools at least for 2.6/3.0


Great !


这篇关于生成可能配置的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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