python - 有没有办法比较 Python 中的列表,类似于 excel 中的 vlookup
问题描述
Python 比较 list1 和 list2
list1 = [1,2,3,4,5,6,7]
list2 = [2,3,4]
list_compare = ['not-found','found','found','found','not-found','not-found','not-found']
“list1”和“list_compare”的长度应该相同,以便将其写入 csv。
解决方案
这是一个使用列表理解的简单单行
list1= [1,2,3,4,5,6,7]
list2= [2,3,4]
compared = ['found' if x in list2 else 'not-found' for x in list1]
print(compared) #['not-found', 'found', 'found', 'found', 'not-found', 'not-found', 'not-found']
推荐阅读
- nestjs - 一对多关系类型
- minecraft - 调试 Minecraft 插件更简单的方法是什么?
- json - 如何在不创建任何列表的情况下检索 Json 参数
- awk - 使用 awk 在一些正则表达式之前打印第 N 条记录
- github - 我可以在 github 工作流程中有条件地运行一些作业,例如仅在特定触发器上运行吗?
- html - 背景图像被“最小高度”截断 - 如何解决?
- github - “现在是一个特殊的存储库:它的 README.md 将出现在您的个人资料中!” 但它不起作用
- 3d - 如何从仅给出几个点的值中插入不规则点云的颜色?
- flutter - Flutter 搭建网页
- scaladoc - Scaladoc 参考文档 - 它存在吗?