Create a Python list with every combination of ‘+’, ‘-‘, ‘*’, and ‘/’ strings.

huangapple go评论65阅读模式
英文:

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.)

huangapple
  • 本文由 发表于 2023年5月22日 00:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定