Raj*_*kar 5 python testing combinations
我正在尝试成对测试并想要基于 Python 的成对测试工具。我已经尝试过 AllPairs(http://pypi.python.org/pypi/AllPairs/2.0.1)。当我在列中提供 10 个条目时,它有错误。目前使用 Microsoft PICT 生成成对组合。
Python 中是否有任何工具可以为大型数组生成成对组合?
AllPairs 中的错误如果我给这个
parameters = [ [ "Brand X", "Brand Y","Brand A","Brand B","Brand C","Brand D" ]
, [ "98", "NT", "2000", "XP"]
, [ "Internal", "Modem","A","B","C","D","E","F","G","H","I","J","K","L","M" ]
, [ "Salaried", "Hourly", "Part-Time", "Contr.","AA","BB","CC","DD","EE","FF","GG","HH","II" ]
, [ 6, 10, 15, 30, 60, 70, 80, 90, 100, 110, 120, 130, 140 ]
]
Run Code Online (Sandbox Code Playgroud)
输出是
Brand X count is 16
Brand Y count is 122
Brand A count is 16
Brand B count is 16
Brand C count is 16
Brand D count is 15
Run Code Online (Sandbox Code Playgroud)
为了这
parameters = [ [ "Brand X", "Brand Y","Brand A","Brand B","Brand C","Brand D" ]
, [ "98", "NT", "2000", "XP"]
, [ "Internal", "Modem" ]
, [ "Salaried", "Hourly", "Part-Time", "Contr." ]
, [ 6, 10, 15, 30, 60 ]
]
Run Code Online (Sandbox Code Playgroud)
输出是
Brand X count is 5
Brand Y count is 5
Brand A count is 5
Brand B count is 5
Brand C count is 5
Brand D count is 6
Run Code Online (Sandbox Code Playgroud)
我认为,它不适用于更大的阵列。
像这样的事情怎么样?
from itertools import chain, combinations, product
def pairwiseGen(*sequences):
unseen = set(chain.from_iterable(product(*i) for i in combinations(sequences, 2)))
for path in product(*sequences):
common_pairs = set(combinations(path, 2)) & unseen
if common_pairs:
yield path
unseen.difference_update(common_pairs)
Run Code Online (Sandbox Code Playgroud)
用法(使用parameters您在问题中定义的列表):
>>> pairs = list(pairwiseGen(*parameters))
>>> len(pairs)
846
Run Code Online (Sandbox Code Playgroud)
我认为仍然有一些优化的空间(上面的生成器产生的结果比预期的稍多),但我想你会同意它比使用笛卡尔积短得多:
>>> all_possible = list(product(*parameters))
>>> len(all_possible)
60480
Run Code Online (Sandbox Code Playgroud)