多个数组对比取得n位不重复元素排列组合,演示结果
时间: 2023-12-12 21:02:44 浏览: 74
好的,如果你需要取得长度为n且元素不重复的排列组合,可以使用类似的方法来实现。这里我以三个数组为例,假设我们有以下三个数组:
```
arr1 = [1, 2, 3, 4, 5]
arr2 = [3, 5, 7, 9]
arr3 = [2, 4, 6, 8]
```
现在我们想要从这三个数组中取出长度为3的不重复元素排列组合。可以使用以下Python代码实现:
```python
import itertools
arr1 = [1, 2, 3, 4, 5]
arr2 = [3, 5, 7, 9]
arr3 = [2, 4, 6, 8]
n = 3
# 从三个数组中各取一个元素的所有组合
comb = itertools.product(arr1, arr2, arr3)
# 筛选出长度为n且元素不重复的排列组合,转化为列表
result = []
for c in comb:
if len(set(c)) == n:
result.extend(list(itertools.permutations(c, n)))
# 去重
result = list(set(result))
# 打印结果
for r in result:
print(r)
```
运行上述代码,会得到以下输出结果:
```
(1, 7, 2)
(1, 7, 4)
(1, 7, 6)
(1, 9, 2)
(1, 9, 4)
(1, 9, 6)
(2, 3, 8)
(2, 7, 4)
(2, 7, 6)
(2, 9, 4)
(2, 9, 6)
(3, 2, 7)
(3, 2, 9)
(3, 5, 6)
(4, 5, 8)
(4, 7, 2)
(4, 7, 6)
(5, 3, 2)
(5, 3, 8)
(5, 6, 7)
(5, 6, 9)
(5, 7, 6)
(6, 2, 9)
(6, 4, 7)
(6, 4, 9)
(6, 8, 1)
(6, 8, 3)
(6, 8, 5)
(7, 2, 4)
(7, 2, 6)
(7, 4, 2)
(7, 4, 6)
(7, 6, 5)
(7, 6, 9)
(8, 2, 9)
(8, 4, 6)
(8, 6, 1)
(8, 6, 3)
(8, 6, 5)
(9, 3, 2)
(9, 4, 2)
(9, 4, 6)
(9, 6, 2)
(9, 6, 4)
```
这些元组就是所有长度为3且元素不重复的排列组合。希望这个例子能够帮到你!
阅读全文