简体   繁体   English

如何获得列表中所有可能的结果等于或小于 2 但等于或大于 1?

[英]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.

相关问题 如何计算大于等于 c 的列表的所有可能子集 - How to count all the possible subsets of a list greater than equal to c 如何在django过滤器中做小于或等于和大于等于? - how to do less than or equal to and greater than equal to in django filter? 如何使 python 大于/小于或等于? - How to make a greater/less than or equal to on python? python 中的“大于”或“等于”与“等于”或“大于” - “Greater than” or “equal” vs “equal” or “greater than” in python 大于或等于浮动失败 - Greater than or equal for floats failed DRF ModelViewSet 查询集返回其日期大于或等于今天的结果 - DRF ModelViewSet queryset return results that their date is greater than or equal to today 列表中的元素大于或等于其他列表中的元素(没有 for 循环?) - Elements in list greater than or equal to elements in other list (without for loop?) 如何使用Python中的字典找到值总和等于或大于k的键的可能组合? - How to find possible combination of keys having sum of values equal or greater than k using dictionary in Python? 如何在 python 中删除列表中大于或等于指定数字的数字 - How do you remove numbers greater than or equal to a specified number within a list, in python 如何检查一个数字的每个数字是否大于或等于另一个数字? - How to check every digit of a number is greater or equal than another number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM