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

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

Create a Python list with every combination of '+', '-', '*', and '/' strings

问题

你可以尝试以下的Python列表推导式来创建你想要的2D测试用例列表:

  1. 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:

  1. [('+', '+', '+', '+'),
  2. ('+', '+', '+', '-'),
  3. ('+', '+', '+', '/'),
  4. ('+', '+', '+', '*'),
  5. ('+', '+', '-', '-'),
  6. ('+', '+', '-', '/'),
  7. ('+', '+', '-', '*'),
  8. ('+', '+', '/', '/'),
  9. ('+', '+', '/', '*'),
  10. ('+', '+', '*', '*'),
  11. ('+', '-', '-', '-'),
  12. ('+', '-', '-', '/'),
  13. ('+', '-', '-', '*'),
  14. ('+', '-', '/', '/'),
  15. ('+', '-', '/', '*'),
  16. ('+', '-', '*', '*'),
  17. ('+', '/', '/', '/'),
  18. ('+', '/', '/', '*'),
  19. ('+', '/', '*', '*'),
  20. ('+', '*', '*', '*'),
  21. ('-', '-', '-', '-'),
  22. ('-', '-', '-', '/'),
  23. ('-', '-', '-', '*'),
  24. ('-', '-', '/', '/'),
  25. ('-', '-', '/', '*'),
  26. ('-', '-', '*', '*'),
  27. ('-', '/', '/', '/'),
  28. ('-', '/', '/', '*'),
  29. ('-', '/', '*', '*'),
  30. ('-', '*', '*', '*'),
  31. ('/', '/', '/', '/'),
  32. ('/', '/', '/', '*'),
  33. ('/', '/', '*', '*'),
  34. ('/', '*', '*', '*'),
  35. ('*', '*', '*', '*')]

I am thinking to create it in a Python list comprehension. I tried:

  1. [[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?

  1. In [1]: from itertools import combinations_with_replacement
  2. In [2]: list(combinations_with_replacement(["+", "-", "/", "*"], 4))
  3. Out[2]:
  4. [('+', '+', '+', '+'),
  5. ('+', '+', '+', '-'),
  6. ('+', '+', '+', '/'),
  7. ('+', '+', '+', '*'),
  8. ('+', '+', '-', '-'),
  9. ...
  10. ( '*', '*', '*', '*')]

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:

  1. from itertools import product
  2. list(product(["+", "-", "/", "*"], repeat=4))

combinations_with_replacement 返回的是这个的一个子集,正如 itertools 文档所述:

> combinations_with_replacement() 的代码也可以被表达为 product() 的一个子序列,经过过滤后,其中元素不按顺序排列

从提供的“等效”代码中,你也可以构建一个纯粹的 Python 实现。关键是将输入的乘法与筛选所需的输出分开:也就是说,这是一个两步骤的过程,而不是一步骤的过程。(尽管这第一步可以实时完成。)

英文:

itertools.combinations_with_replacement?

  1. In [1]: from itertools import combinations_with_replacement
  2. In [2]: list(combinations_with_replacement(["+", "-", "/", "*"], 4))
  3. Out[2]:
  4. [('+', '+', '+', '+'),
  5. ('+', '+', '+', '-'),
  6. ('+', '+', '+', '/'),
  7. ('+', '+', '+', '*'),
  8. ('+', '+', '-', '-'),
  9. ('+', '+', '-', '/'),
  10. ('+', '+', '-', '*'),
  11. ('+', '+', '/', '/'),
  12. ('+', '+', '/', '*'),
  13. ('+', '+', '*', '*'),
  14. ('+', '-', '-', '-'),
  15. ('+', '-', '-', '/'),
  16. ('+', '-', '-', '*'),
  17. ('+', '-', '/', '/'),
  18. ('+', '-', '/', '*'),
  19. ('+', '-', '*', '*'),
  20. ('+', '/', '/', '/'),
  21. ('+', '/', '/', '*'),
  22. ('+', '/', '*', '*'),
  23. ('+', '*', '*', '*'),
  24. ('-', '-', '-', '-'),
  25. ('-', '-', '-', '/'),
  26. ('-', '-', '-', '*'),
  27. ('-', '-', '/', '/'),
  28. ('-', '-', '/', '*'),
  29. ('-', '-', '*', '*'),
  30. ('-', '/', '/', '/'),
  31. ('-', '/', '/', '*'),
  32. ('-', '/', '*', '*'),
  33. ('-', '*', '*', '*'),
  34. ('/', '/', '/', '/'),
  35. ('/', '/', '/', '*'),
  36. ('/', '/', '*', '*'),
  37. ('/', '*', '*', '*'),
  38. ('*', '*', '*', '*')]

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:

  1. from itertools import product
  2. 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:

确定