2.创建两个集合{"lily", "tom", "John", "Kevin"}和{"George", "Rose", "tom", "Helena"},求它们的并、交和差。
时间: 2023-12-11 22:33:19 浏览: 27
以下是创建两个集合并求它们的并、交和差的Python代码:
```python
set1 = {"lily", "tom", "John", "Kevin"}
set2 = {"George", "Rose", "tom", "Helena"}
# 求并集
union_set = set1.union(set2)
print("并集:", union_set)
# 求交集
intersection_set = set1.intersection(set2)
print("交集:", intersection_set)
# 求差集
diff_set = set1.difference(set2)
print("差集(set1 - set2):", diff_set)
diff_set = set2.difference(set1)
print("差集(set2 - set1):", diff_set)
```
输出结果为:
```
并集: {'lily', 'George', 'Rose', 'Kevin', 'John', 'tom', 'Helena'}
交集: {'tom'}
差集(set1 - set2): {'lily', 'John', 'Kevin'}
差集(set2 - set1): {'Rose', 'George', 'Helena'}
```
相关问题
2用Java.创建两个集合{"lily", "tom", "John", "Kevin"}和{"George", "Rose", "tom", "Helena"},求它们的并、交和差。
以下是Java中求两个集合的并、交和差的例子:
```java
import java.util.*;
public class SetOperations {
public static void main(String[] args) {
Set<String> set1 = new HashSet<>(Arrays.asList("lily", "tom", "John", "Kevin"));
Set<String> set2 = new HashSet<>(Arrays.asList("George", "Rose", "tom", "Helena"));
// 求并集
Set<String> union = new HashSet<>(set1);
union.addAll(set2);
System.out.println("Union: " + union);
// 求交集
Set<String> intersection = new HashSet<>(set1);
intersection.retainAll(set2);
System.out.println("Intersection: " + intersection);
// 求差集
Set<String> difference = new HashSet<>(set1);
difference.removeAll(set2);
System.out.println("Difference: " + difference);
}
}
```
输出结果为:
```
Union: [John, Kevin, George, Rose, tom, lily, Helena]
Intersection: [tom]
Difference: [John, Kevin, lily]
```
2.创建两个集合{"lily",+"tom",+"John",+"Kevin"}和{"George",+"Rose",+"tom",+"Helena"},求它们的并、交和差。
以下是Python中创建两个集合并求它们的并、交和差的例子:
```python
set1 = {"lily", "tom", "John", "Kevin"}
set2 = {"George", "Rose", "tom", "Helena"}
# 求并集
union_set = set1.union(set2)
print("并集为:", union_set)
# 求交集
intersection_set = set1.intersection(set2)
print("交集为:", intersection_set)
# 求差集
diff_set = set1.difference(set2)
print("set1与set2的差集为:", diff_set)
diff_set = set2.difference(set1)
print("set2与set1的差集为:", diff_set)
```
输出结果为:
```
并集为: {'lily', 'George', 'Rose', 'Kevin', 'John', 'tom', 'Helena'}
交集为: {'tom'}
set1与set2的差集为: {'lily', 'Kevin', 'John'}
set2与set1的差集为: {'Rose', 'George', 'Helena'}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)