1. arr1 = [1,2,3,4,5,6,78,8] 2. arr2 = [1,2,4,5,777,98]实现数组交集并集和差集
时间: 2023-08-19 09:26:46 浏览: 143
电子-STM32ZET6TIM1TIM8PWM1PWM4.doc
好的,针对你的问题,我可以提供以下实现:
1. 数组交集:
```python
arr1 = [1,2,3,4,5,6,78,8]
arr2 = [1,2,4,5,777,98]
intersection = list(set(arr1).intersection(set(arr2)))
print(intersection)
```
输出结果为:`[1, 2, 4, 5]`
2. 数组并集:
```python
arr1 = [1,2,3,4,5,6,78,8]
arr2 = [1,2,4,5,777,98]
union = list(set(arr1).union(set(arr2)))
print(union)
```
输出结果为:`[1, 2, 3, 4, 5, 6, 98, 777, 8, 78]`
3. 数组差集:
```python
arr1 = [1,2,3,4,5,6,78,8]
arr2 = [1,2,4,5,777,98]
diff_arr1 = list(set(arr1).difference(set(arr2)))
diff_arr2 = list(set(arr2).difference(set(arr1)))
print("arr1中独有的元素:", diff_arr1)
print("arr2中独有的元素:", diff_arr2)
```
输出结果为:
```
arr1中独有的元素: [8, 3, 6, 78]
arr2中独有的元素: [98, 777]
```
希望我的回答对你有所帮助!
阅读全文