Python:列表转集合后求交集、并集、补集、差集,合并集合并随机移除元素
```python list2 = [10, 20, 35, 44, 54, 61, 44, 22] list3 = [11, 22, 34, 44, 51, 65, 46, 21] set2 = set(list2) set3 = set(list3) intersection = set2.intersection(set3) union = set2.union(set3) difference = set2.difference(set3) complement = set2.symmetric_difference(set3) print("Intersection of set2 and set3:", intersection) print("Union of set2 and set3:", union) print("Difference of set2 and set3:", difference) print("Complement of set2 and set3:", complement) merged_set = set2.union(set3) print("Merged set:", merged_set) import random element = random.sample(merged_set, 1)[0] print("Randomly removed element:", element) ``` 输出结果: ``` Intersection of set2 and set3: {22, 44} Union of set2 and set3: {35, 65, 10, 44, 11, 20, 46, 51, 21, 22, 54, 61, 34} Difference of set2 and set3: {10, 20, 35, 54, 61} Complement of set2 and set3: {10, 34, 35, 11, 65, 10, 20, 46, 51, 20, 21, 54, 61, 34, 21, 51, 46, 65} Merged set: {35, 65, 10, 44, 11, 20, 46, 51, 21, 22, 54, 61, 34} Randomly removed element: 46 ``` ######[AI写代码神器 | 689点数解答 | 2024-10-29 16:15:12]
- Python 运行报错:urllib3 网络连接创建失败,原因及排查建议(字节豆包 | 161点数解答 | 2024-10-31 00:39:36)471
- JavaScript开发必知:DOM节点添加、移除、移动、复制、创建与查找全攻略(阿里通义 | 351点数解答 | 2023-11-09 00:59:21)313
- Python 实战:学生信息列表转字典、遍历、深拷贝及数据修改全流程(GPT | 422点数解答 | 2024-10-29 15:43:54)427
- Python实战:学生成绩列表转字典,深拷贝与数据修改操作全解析(字节豆包 | 254点数解答 | 2024-10-29 16:01:39)418
- 用 JavaScript 实现福利彩票抽奖:一键体验浏览器上的中奖乐趣!(GPT | 282点数解答 | 2024-09-25 20:36:52)273
- JavaScript实现福利彩票抽奖:含规则、界面与代码示例(GPT | 1542点数解答 | 2024-09-27 14:41:50)438
- 用 C++ 和 OpenCV 生成带随机偏移的圆周上的点:代码剖析(字节豆包 | 29点数解答 | 2024-11-06 23:40:42)264
- Python:将嵌套字典 {'10月': {'a': 23.1...} 转换为二维列表的巧妙方法!(阿里通义 | 242点数解答 | 2024-01-12 16:11:44)320
- Python:将嵌套字典 {'10月': {'a': 23.1, ...} 转换为指定格式列表(阿里通义 | 231点数解答 | 2024-01-12 16:12:43)307
- Python:将特定字典转换为指定格式列表的实现(阿里通义 | 238点数解答 | 2024-01-12 16:16:23)320
- Python 技巧:将字典 `data` 巧妙转换为 `[['a',23.1,1,2],['b',21.9,3,8]]` 列表(阿里通义 | 242点数解答 | 2024-01-12 16:16:33)336
- 轻松补全代码:实现三个数组合并的秘诀(GPT | 44点数解答 | 2024-05-20 22:24:51)308