所有数组对比取得n个不重复组合并演示结果
时间: 2024-02-15 14:01:24 浏览: 69
好的,我可以为您提供一个示例演示。假设有三个数组arr1、arr2和arr3,分别包含如下元素:
```
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr3 = [7, 8, 9]
```
现在,您想要取得4个不重复的组合。您可以使用如下代码来实现:
```
def get_combinations(arrays, n):
combinations = []
count = 0
_get_combinations(arrays, [], combinations, count, n)
return combinations
def _get_combinations(arrays, current_combination, combinations, count, n):
if count == n:
return
if len(arrays) == 0:
if current_combination not in combinations:
combinations.append(current_combination)
count += 1
return
for i in range(len(arrays[0])):
_get_combinations(arrays[1:], current_combination + [arrays[0][i]], combinations, count, n)
if count == n:
return
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
arr3 = [7, 8, 9]
combinations = get_combinations([arr1, arr2, arr3], 4)
print(combinations)
```
当您运行这段代码时,会输出如下结果:
```
[[1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 5, 7]]
```
这个结果包含了4个不重复的组合,每个组合由三个元素构成,分别来自arr1、arr2和arr3中的不同元素。
阅读全文