是的,你说得对。set()函数可以用于集合的运算,包括求交集、并集、差集等。下面是一些示例:
1. 求两个集合的交集:使用 & 运算符或intersection()方法
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection = set1 & set2
print(intersection) # {2, 3}
```
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection = set1.intersection(set2)
print(intersection) # {2, 3}
```
2. 求两个集合的并集:使用 | 运算符或union()方法
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union = set1 | set2
print(union) # {1, 2, 3, 4}
```
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union = set1.union(set2)
print(union) # {1, 2, 3, 4}
```
3. 求两个集合的差集:使用 - 运算符或difference()方法
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference = set1 - set2
print(difference) # {1}
```
```python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference = set1.difference(set2)
print(difference) # {1}
```
希望这些示例对你有所帮助。