[英]how to get all possible results in list that equal or smaller than 2 but equal or greater than 1?
For example: To find all possible lists with length=3, and all elements are equal or smaller than 2 but equal or greater than 1. (you can use numpy)例如:查找所有长度为3的可能列表,并且所有元素都等于或小于2但等于或大于1。(可以使用numpy)
input: len = 3, m = 1, n=2
output:[[1,1,1],[1,1,2],[1,2,1],[2,1,1],[2,2,1],[2,1,2],[1,2,2],[2,2,2]]
(it's a little bit like np.random.randint, but we hope to get all results) (有点像np.random.randint,但是我们希望得到所有的结果)
Probably not the best way but you could do this:可能不是最好的方法,但你可以这样做:
from itertools import permutations
s = [list(t) for t in {p for p in permutations([1, 1, 1, 2, 2, 2], 3)}]
print(s)
Output: Output:
[[1, 2, 1], [2, 1, 1], [2, 2, 2], [1, 1, 2], [1, 2, 2], [2, 1, 2], [2, 2, 1], [1, 1, 1]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.