英文:
Create a Python list with every combination of '+', '-', '*', and '/' strings
问题
你可以尝试以下的Python列表推导式来创建你想要的2D测试用例列表:
test_cases = [(a, b, c, d) for a in ('+', '-', '*', '/') for b in ('+', '-', '*', '/') for c in ('+', '-', '*', '/') for d in ('+', '-', '*', '/')]
这将生成包含所有可能情况的测试用例列表。
英文:
I am trying to build a 2d list of test cases that contains all the possible cases ('+', '-', '*', '/') like this:
[('+', '+', '+', '+'),
('+', '+', '+', '-'),
('+', '+', '+', '/'),
('+', '+', '+', '*'),
('+', '+', '-', '-'),
('+', '+', '-', '/'),
('+', '+', '-', '*'),
('+', '+', '/', '/'),
('+', '+', '/', '*'),
('+', '+', '*', '*'),
('+', '-', '-', '-'),
('+', '-', '-', '/'),
('+', '-', '-', '*'),
('+', '-', '/', '/'),
('+', '-', '/', '*'),
('+', '-', '*', '*'),
('+', '/', '/', '/'),
('+', '/', '/', '*'),
('+', '/', '*', '*'),
('+', '*', '*', '*'),
('-', '-', '-', '-'),
('-', '-', '-', '/'),
('-', '-', '-', '*'),
('-', '-', '/', '/'),
('-', '-', '/', '*'),
('-', '-', '*', '*'),
('-', '/', '/', '/'),
('-', '/', '/', '*'),
('-', '/', '*', '*'),
('-', '*', '*', '*'),
('/', '/', '/', '/'),
('/', '/', '/', '*'),
('/', '/', '*', '*'),
('/', '*', '*', '*'),
('*', '*', '*', '*')]
I am thinking to create it in a Python list comprehension. I tried:
[[x] * 4 for x in ('+','-','*', '/')]
but the result is not something I want. Anyone knows how to do it? Thanks.
答案1
得分: 2
itertools.combinations_with_replacement
?
In [1]: from itertools import combinations_with_replacement
In [2]: list(combinations_with_replacement(["+", "-", "/", "*"], 4))
Out[2]:
[('+', '+', '+', '+'),
('+', '+', '+', '-'),
('+', '+', '+', '/'),
('+', '+', '+', '*'),
('+', '+', '-', '-'),
...
( '*', '*', '*', '*')]
This assumes, as @ayhan points out, that order doesn't matter. If order does matter, you just want the cartesian product, with a repeated input:
from itertools import product
list(product(["+", "-", "/", "*"], repeat=4))
combinations_with_replacement
返回的是这个的一个子集,正如 itertools 文档所述:
> combinations_with_replacement() 的代码也可以被表达为 product() 的一个子序列,经过过滤后,其中元素不按顺序排列
从提供的“等效”代码中,你也可以构建一个纯粹的 Python 实现。关键是将输入的乘法与筛选所需的输出分开:也就是说,这是一个两步骤的过程,而不是一步骤的过程。(尽管这第一步可以实时完成。)
英文:
itertools.combinations_with_replacement
?
In [1]: from itertools import combinations_with_replacement
In [2]: list(combinations_with_replacement(["+", "-", "/", "*"], 4))
Out[2]:
[('+', '+', '+', '+'),
('+', '+', '+', '-'),
('+', '+', '+', '/'),
('+', '+', '+', '*'),
('+', '+', '-', '-'),
('+', '+', '-', '/'),
('+', '+', '-', '*'),
('+', '+', '/', '/'),
('+', '+', '/', '*'),
('+', '+', '*', '*'),
('+', '-', '-', '-'),
('+', '-', '-', '/'),
('+', '-', '-', '*'),
('+', '-', '/', '/'),
('+', '-', '/', '*'),
('+', '-', '*', '*'),
('+', '/', '/', '/'),
('+', '/', '/', '*'),
('+', '/', '*', '*'),
('+', '*', '*', '*'),
('-', '-', '-', '-'),
('-', '-', '-', '/'),
('-', '-', '-', '*'),
('-', '-', '/', '/'),
('-', '-', '/', '*'),
('-', '-', '*', '*'),
('-', '/', '/', '/'),
('-', '/', '/', '*'),
('-', '/', '*', '*'),
('-', '*', '*', '*'),
('/', '/', '/', '/'),
('/', '/', '/', '*'),
('/', '/', '*', '*'),
('/', '*', '*', '*'),
('*', '*', '*', '*')]
This assumes, as @ayhan points out, that order doesn't matter. If order does matter, you just want the cartesian product, with a repeated input:
from itertools import product
list(product(["+", "-", "/", "*"], repeat=4))
combinations_with_replacement
returns a subset of this, as the itertools docs note
> The code for combinations_with_replacement() can be also expressed as a subsequence of product() after filtering entries where the elements are not in sorted order
From the provided 'equivalent' code you can also build up a pure python implementation. The trick is separating the multiplying of inputs from filtering the desired outputs: i.e. it's a two step, not one step process. (Albeit this first step can be done on the fly.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论