1. 输入A和B元素个数不易太多(不超过10个),一般4到5个就行; 2. 输出结果要有:A与B的交集、A与B的并集、A-B、B-A,A与B的对称差。 3. 测试如下两个例子的结果: (1) 集合A = {6, 3, 7, 4, 12},B={3, 5, 4 ,9} (2) 集合A = {4, 6, 8},B={6, 4, 10 ,1, 8},A-B=
时间: 2024-02-27 09:57:05 浏览: 56
好的,我能理解您的问题。以下是我提供的代码,实现了您所需的集合操作:
```python
def set_operations(A, B):
intersection = set(A) & set(B)
union = set(A) | set(B)
A_diff_B = set(A) - set(B)
B_diff_A = set(B) - set(A)
symmetric_diff = set(A) ^ set(B)
return intersection, union, A_diff_B, B_diff_A, symmetric_diff
# 测试代码
A = [6, 3, 7, 4, 12]
B = [3, 5, 4, 9]
intersection, union, A_diff_B, B_diff_A, symmetric_diff = set_operations(A, B)
print(f"A = {A}")
print(f"B = {B}")
print(f"Intersection of A and B: {intersection}")
print(f"Union of A and B: {union}")
print(f"A - B: {A_diff_B}")
print(f"B - A: {B_diff_A}")
print(f"Symmetric difference of A and B: {symmetric_diff}")
A = [4, 6, 8]
B = [6, 4, 10, 1, 8]
intersection, union, A_diff_B, B_diff_A, symmetric_diff = set_operations(A, B)
print(f"A = {A}")
print(f"B = {B}")
print(f"Intersection of A and B: {intersection}")
print(f"Union of A and B: {union}")
print(f"A - B: {A_diff_B}")
print(f"B - A: {B_diff_A}")
print(f"Symmetric difference of A and B: {symmetric_diff}")
```
输出结果如下:
```
A = [6, 3, 7, 4, 12]
B = [3, 5, 4, 9]
Intersection of A and B: {3, 4}
Union of A and B: {3, 4, 5, 6, 7, 9, 12}
A - B: {6, 7, 12}
B - A: {9, 5}
Symmetric difference of A and B: {5, 6, 7, 9, 12}
A = [4, 6, 8]
B = [6, 4, 10, 1, 8]
Intersection of A and B: {8, 4, 6}
Union of A and B: {1, 4, 6, 8, 10}
A - B: set()
B - A: {1, 10}
Symmetric difference of A and B: {1, 10}
```
希望这个代码能够满足您的需求!
阅读全文