简体   繁体   English

我如何找到所有可能的方法,我可以将一个数组安装到 4 个插槽中,并且数组可能有更多/少于 4 个数字?

[英]How do I find all possible ways I can fit an array to 4 slots with the possibility of the array having more/less than 4 numbers?

Let's say I have 4 buckets and an array of numbers like [1,2,3,4,5]假设我有 4 个桶和一组数字,例如[1,2,3,4,5]

|  ||  ||  ||  |
|__||__||__||__|
 1   2   3   4
 5   2   3   4
 1   5   3   4     
 etc...

I can also have less than 4 numbers like [1, 2, 3]我也可以有少于 4 个数字,例如[1, 2, 3]

|  ||  ||  ||  |
|__||__||__||__|
 1   2   3   
 1   3   2   
     1   2   3     
 etc...

How do I find all possible combinations of the numbers in buckets (like [1,2,3,4] if length is >= 4 or [1,2,None,None] if length is < 4)?如何找到桶中数字的所有可能组合(如[1,2,3,4]如果长度 >= 4 或[1,2,None,None]如果长度 < 4)?

You can use a recursive generator function:您可以使用递归生成器 function:

def combos(nums, buckets, c = []):
  if len(c) == buckets:
    yield c
  else:
    if len(c) + len(nums) < buckets:
      yield from combos(nums, buckets, c+[None])
    for i, a in enumerate(nums):
      yield from combos(nums[:i]+nums[i+1:], buckets, c+[a])

print(list(combos([1, 2, 3, 4, 5], 4)))
print(list(combos([1, 2, 3], 4)))

Output: Output:

[[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 3], [1, 2, 4, 5], [1, 2, 5, 3], [1, 2, 5, 4], [1, 3, 2, 4], [1, 3, 2, 5], [1, 3, 4, 2], [1, 3, 4, 5], [1, 3, 5, 2], [1, 3, 5, 4], [1, 4, 2, 3], [1, 4, 2, 5], [1, 4, 3, 2], [1, 4, 3, 5], [1, 4, 5, 2], [1, 4, 5, 3], [1, 5, 2, 3], [1, 5, 2, 4], [1, 5, 3, 2], [1, 5, 3, 4], [1, 5, 4, 2], [1, 5, 4, 3], [2, 1, 3, 4], [2, 1, 3, 5], [2, 1, 4, 3], [2, 1, 4, 5], [2, 1, 5, 3], [2, 1, 5, 4], [2, 3, 1, 4], [2, 3, 1, 5], [2, 3, 4, 1], [2, 3, 4, 5], [2, 3, 5, 1], [2, 3, 5, 4], [2, 4, 1, 3], [2, 4, 1, 5], [2, 4, 3, 1], [2, 4, 3, 5], [2, 4, 5, 1], [2, 4, 5, 3], [2, 5, 1, 3], [2, 5, 1, 4], [2, 5, 3, 1], [2, 5, 3, 4], [2, 5, 4, 1], [2, 5, 4, 3], [3, 1, 2, 4], [3, 1, 2, 5], [3, 1, 4, 2], [3, 1, 4, 5], [3, 1, 5, 2], [3, 1, 5, 4], [3, 2, 1, 4], [3, 2, 1, 5], [3, 2, 4, 1], [3, 2, 4, 5], [3, 2, 5, 1], [3, 2, 5, 4], [3, 4, 1, 2], [3, 4, 1, 5], [3, 4, 2, 1], [3, 4, 2, 5], [3, 4, 5, 1], [3, 4, 5, 2], [3, 5, 1, 2], [3, 5, 1, 4], [3, 5, 2, 1], [3, 5, 2, 4], [3, 5, 4, 1], [3, 5, 4, 2], [4, 1, 2, 3], [4, 1, 2, 5], [4, 1, 3, 2], [4, 1, 3, 5], [4, 1, 5, 2], [4, 1, 5, 3], [4, 2, 1, 3], [4, 2, 1, 5], [4, 2, 3, 1], [4, 2, 3, 5], [4, 2, 5, 1], [4, 2, 5, 3], [4, 3, 1, 2], [4, 3, 1, 5], [4, 3, 2, 1], [4, 3, 2, 5], [4, 3, 5, 1], [4, 3, 5, 2], [4, 5, 1, 2], [4, 5, 1, 3], [4, 5, 2, 1], [4, 5, 2, 3], [4, 5, 3, 1], [4, 5, 3, 2], [5, 1, 2, 3], [5, 1, 2, 4], [5, 1, 3, 2], [5, 1, 3, 4], [5, 1, 4, 2], [5, 1, 4, 3], [5, 2, 1, 3], [5, 2, 1, 4], [5, 2, 3, 1], [5, 2, 3, 4], [5, 2, 4, 1], [5, 2, 4, 3], [5, 3, 1, 2], [5, 3, 1, 4], [5, 3, 2, 1], [5, 3, 2, 4], [5, 3, 4, 1], [5, 3, 4, 2], [5, 4, 1, 2], [5, 4, 1, 3], [5, 4, 2, 1], [5, 4, 2, 3], [5, 4, 3, 1], [5, 4, 3, 2]]
[[None, 1, 2, 3], [None, 1, 3, 2], [None, 2, 1, 3], [None, 2, 3, 1], [None, 3, 1, 2], [None, 3, 2, 1], [1, None, 2, 3], [1, None, 3, 2], [1, 2, None, 3], [1, 2, 3, None], [1, 3, None, 2], [1, 3, 2, None], [2, None, 1, 3], [2, None, 3, 1], [2, 1, None, 3], [2, 1, 3, None], [2, 3, None, 1], [2, 3, 1, None], [3, None, 1, 2], [3, None, 2, 1], [3, 1, None, 2], [3, 1, 2, None], [3, 2, None, 1], [3, 2, 1, None]]

At each recursive call, if a combination has not yet been formed, the code does two things:在每次递归调用中,如果还没有形成组合,代码会做两件事:

  1. Checks if the specified number of buckets is greater than the input number list.检查指定的桶数是否大于输入数列表。 If so, then the output is padded with None .如果是这样,那么 output 用None填充。
  2. The remaining number list is iterated over, and each iteration value is added to the running result, and the recursion proceeds.对剩余的数列表进行迭代,将每个迭代值加到运行结果中,继续递归。

Use the built-in function itertools.permutations .使用内置的 function itertools.permutations

import itertools


def func(arr: list, slot_count=4):
    if (v := slot_count - len(arr)) > 0:
        arr.extend([None for _ in range(v)])
    return itertools.permutations(arr, slot_count)


print(list(func([1, 2, 3])))

print(list(func([1, 2, 3, 4, 5])))
[(1, 2, 3, None), (1, 2, None, 3), (1, 3, 2, None), (1, 3, None, 2), (1, None, 2, 3), (1, None, 3, 2), (2, 1, 3, None), (2, 1, None, 3), (2, 3, 1, None), (2, 3, None, 1), (2, None, 1, 3), (2, None, 3, 1), (3, 1, 2, None), (3, 1, None, 2), (3, 2, 1, None), (3, 2, None, 1), (3, None, 1, 2), (3, None, 2, 1), (None, 1, 2, 3), (None, 1, 3, 2), (None, 2, 1, 3), (None, 2, 3, 1), (None, 3, 1, 2), (None, 3, 2, 1)]
[(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 3), (1, 2, 4, 5), (1, 2, 5, 3), (1, 2, 5, 4), (1, 3, 2, 4), (1, 3, 2, 5), (1, 3, 4, 2), (1, 3, 4, 5), (1, 3, 5, 2), (1, 3, 5, 4), (1, 4, 2, 3), (1, 4, 2, 5), (1, 4, 3, 2), (1, 4, 3, 5), (1, 4, 5, 2), (1, 4, 5, 3), (1, 5, 2, 3), (1, 5, 2, 4), (1, 5, 3, 2), (1, 5, 3, 4), (1, 5, 4, 2), (1, 5, 4, 3), (2, 1, 3, 4), (2, 1, 3, 5), (2, 1, 4, 3), (2, 1, 4, 5), (2, 1, 5, 3), (2, 1, 5, 4), (2, 3, 1, 4), (2, 3, 1, 5), (2, 3, 4, 1), (2, 3, 4, 5), (2, 3, 5, 1), (2, 3, 5, 4), (2, 4, 1, 3), (2, 4, 1, 5), (2, 4, 3, 1), (2, 4, 3, 5), (2, 4, 5, 1), (2, 4, 5, 3), (2, 5, 1, 3), (2, 5, 1, 4), (2, 5, 3, 1), (2, 5, 3, 4), (2, 5, 4, 1), (2, 5, 4, 3), (3, 1, 2, 4), (3, 1, 2, 5), (3, 1, 4, 2), (3, 1, 4, 5), (3, 1, 5, 2), (3, 1, 5, 4), (3, 2, 1, 4), (3, 2, 1, 5), (3, 2, 4, 1), (3, 2, 4, 5), (3, 2, 5, 1), (3, 2, 5, 4), (3, 4, 1, 2), (3, 4, 1, 5), (3, 4, 2, 1), (3, 4, 2, 5), (3, 4, 5, 1), (3, 4, 5, 2), (3, 5, 1, 2), (3, 5, 1, 4), (3, 5, 2, 1), (3, 5, 2, 4), (3, 5, 4, 1), (3, 5, 4, 2), (4, 1, 2, 3), (4, 1, 2, 5), (4, 1, 3, 2), (4, 1, 3, 5), (4, 1, 5, 2), (4, 1, 5, 3), (4, 2, 1, 3), (4, 2, 1, 5), (4, 2, 3, 1), (4, 2, 3, 5), (4, 2, 5, 1), (4, 2, 5, 3), (4, 3, 1, 2), (4, 3, 1, 5), (4, 3, 2, 1), (4, 3, 2, 5), (4, 3, 5, 1), (4, 3, 5, 2), (4, 5, 1, 2), (4, 5, 1, 3), (4, 5, 2, 1), (4, 5, 2, 3), (4, 5, 3, 1), (4, 5, 3, 2), (5, 1, 2, 3), (5, 1, 2, 4), (5, 1, 3, 2), (5, 1, 3, 4), (5, 1, 4, 2), (5, 1, 4, 3), (5, 2, 1, 3), (5, 2, 1, 4), (5, 2, 3, 1), (5, 2, 3, 4), (5, 2, 4, 1), (5, 2, 4, 3), (5, 3, 1, 2), (5, 3, 1, 4), (5, 3, 2, 1), (5, 3, 2, 4), (5, 3, 4, 1), (5, 3, 4, 2), (5, 4, 1, 2), (5, 4, 1, 3), (5, 4, 2, 1), (5, 4, 2, 3), (5, 4, 3, 1), (5, 4, 3, 2)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在差小于或等于K的数组中找到所有数字 - Find all the numbers in an array whoes difference less than or equal to K 查找小于或等于用户使用循环输入的数字的素数。 我怎么做? - Find prime numbers less than or equal to the number the user has entered with loops. How do I do that? 计算我可以在数组中拥有唯一数字的方法数 - Counting number of ways I can have unique numbers in array 我如何找到所有可能的方法来组合列表中的项目而无需重复? - How do I find all possible ways to combine items in a list without repetition? 如何用除零(Python,numpy)以外的其他数组的所有数字替换数组的所有数字? - How can I replace all numbers of an array with all numbers of an other array except of the zeros (Python, numpy)? 我怎样才能用这个 keras 匹配一个数组 model - How can I fit an array in keras with this model 如何找到所有点的2D numpy数组中的点大于参考数组中的对应点的百分比? - How can I find the percentage of times that a point in a 2D numpy array is greater than the corresponding point in a reference array, for all points? 如何找到具有两位以上小数的浮点数? (Python) - How can I find float numbers that have more than two decimals? (Python) 如何找到小于x的最大整数? - How do I find the largest integer less than x? 我怎样才能得到所有可能的日志级别的数组? - How can I get an array of all possible log levels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM