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.
>Solution :
itertools.combinations_with_replacement
?
In [1]: from itertools import combinations_with_replacement
In [2]: list(combinations_with_replacement(["+", "-", "/", "*"], 4))
Out[2]:
[('+', '+', '+', '+'),
('+', '+', '+', '-'),
('+', '+', '+', '/'),
('+', '+', '+', '*'),
('+', '+', '-', '-'),
('+', '+', '-', '/'),
('+', '+', '-', '*'),
('+', '+', '/', '/'),
('+', '+', '/', '*'),
('+', '+', '*', '*'),
('+', '-', '-', '-'),
('+', '-', '-', '/'),
('+', '-', '-', '*'),
('+', '-', '/', '/'),
('+', '-', '/', '*'),
('+', '-', '*', '*'),
('+', '/', '/', '/'),
('+', '/', '/', '*'),
('+', '/', '*', '*'),
('+', '*', '*', '*'),
('-', '-', '-', '-'),
('-', '-', '-', '/'),
('-', '-', '-', '*'),
('-', '-', '/', '/'),
('-', '-', '/', '*'),
('-', '-', '*', '*'),
('-', '/', '/', '/'),
('-', '/', '/', '*'),
('-', '/', '*', '*'),
('-', '*', '*', '*'),
('/', '/', '/', '/'),
('/', '/', '/', '*'),
('/', '/', '*', '*'),
('/', '*', '*', '*'),
('*', '*', '*', '*')]