python - 两个列表中的值
问题描述
所以我有两个清单。例如:
list1 = [a1, b2, c3, f6]
list2 = [a1, b2, d4, e5]
而且我只希望在 list2 中唯一出现的值附加到新列表中。例如:
list3 = [d4, e5]
解决方案
你可以使用一个list comprehension
.
list3 = [item for item in list2 if item not in list1]
输出
list3 = ['d4', 'e5']
推荐阅读
- python - 与函数参数作斗争
- excel - 如何将命令行参数传递给在 VBA 中使用 Exec 函数调用的 bash 脚本?
- php - 如何使用 PHP curl 将客户端凭据作为 grant_type 来获取 OAuth 2.0?
- java - 由于某种原因,数据没有从第三个活动发送到主活动,并且应用程序崩溃,而第二个活动工作得很好
- java - 具有多于一级符号链接的 Java 中 Path.toRealPath() 的行为
- signal-processing - 用于音频信号处理的 DSP 和传感器信号处理之间是否存在根本区别?
- mvvm - Xamarin Forms - 设置控件模板的 BindingContext
- python - python通过正则表达式和列表理解从字符串中提取数字值
- javascript - 将标签中的文本更改为选择菜单中的选项
- c++ - 如果我尝试在 C++ 中打印类的非静态成员函数的地址,为什么会创建一个对象?