python取两个列表的并集、交集、差集
# 交集
def list_intersection(list1: list, list2: list) -> list:return list(set(list1).intersection(set(list2)))# 并集
def list_union(list1: list, list2: list) -> list:return list(set(list1).union(set(list2)))# 差集(在in_list中,但是不在not_in_list中)
def list_difference(in_list: list, not_in_list: list) -> list:return list(set(in_list).difference(set(not_in_list)))if __name__ == '__main__':list_1 = ['a', 'b', 'c', 1, 2, 3]list_2 = [1, 2, 3, 4, 5, 6]print("交集:", list_intersection(list_1, list_2))print('并集:', list_union(list_1, list_2))print('差集(在list_1中,但不在list_2中):', list_difference(list_1, list_2))
python取两个列表的并集、交集、差集相关推荐
- python求两个列表的并集.交集.差集方法
求两个列表的差集 >>> a = [1,2,3] >>> b=[1,2] >>> ################################ ...
- 使用Set求两个数组的并集|| 交集||差集
这里要讲解一下,使用Set求两个数组的并集|| 交集||差集. 先定义两个数组: let a = new Set([1, 2, 3]);let b = new Set([4, 3, 2]); 并集 l ...
- python 取两/多个list的交集
python 取两个/多个list的交集 Two List Input: lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2 = [9, 4, 5, 36, 47 ...
- python取两个set的并集、交集、差集
文章目录 1. 并集 2. 交集 3. 差集 对于以下两个set x = set([1,2,3]) y = set([2,3,4]) 1. 并集 方法一: x | y 方法二: x.union(y) ...
- python中怎么取两个列表 集合的交集
在python 3.x 版本中 set 中有函数intersection() intersection() 方法用于返回两个或更多集合中都包含的元素,即交集. 语法: 在这里插入代码片set.inte ...
- 高级JAVA - 利用函数式接口实现通用的取并集/交集/差集进阶版
在前文(高级JAVA - 利用函数式接口实现通用的取并集/交集/差集)中我们实现了利用函数式接口获取交集/差集 , 但是只能全部生成 , 假如我们只需要一个交集的话 , 所有代码仍然会执行一次 . 比 ...
- 高级JAVA - 利用函数式接口实现通用的取并集/交集/差集
package com.xing.dto;import com.xing.common.utils.XDataUtil; import lombok.Data;import java.util.Arr ...
- Python中将两个列表数据zip起来并遍历(Iterating through two lists in parallel)
Python中将两个列表数据zip起来并遍历(Iterating through two lists in parallel) 目录 Python中将两个列表数据zip起来并遍历(Iterating ...
- Python中将两个列表(list)数据zip起来
Python中将两个列表(list)数据zip起来 目录 Python中将两个列表(list)数据zip起来
最新文章
- Faster R-CNN教程
- java四种线程池类型以及可选择的阻塞队列
- SQL查询效率:100w数据查询只需要1秒钟
- LightGBM参数的贝叶斯搜索(有boost_from_average参数设置)
- 递归和分治思想及其应用
- python beautifulsoup4 table tr_python BeautifulSoup解析表
- java蓝桥杯 试题-基础练习-数列排序
- python报表_Python生成报表
- iis configuration error
- 麻省理工学生令计算机系统升级不需重启
- 《jBPM4工作流应用开发指南》这本书
- 格雷码的FPGA实现
- 鸿蒙系统我的二次元之旅,创世神的二次元
- win11java环境配置
- java学生成绩统计
- React Or Vue
- 使用Java在浏览器页面无法导出excel表格
- RabbitMQ实战指南之RabbitMQ架构及运转流程
- 关于“马甲app”你不得不知的一些常识
- 树莓派3b+快速编译opencv成功案例指导(保姆级教程)