python - Python中的交叉口保持秩序
问题描述
我想找到两个具有原始顺序的列表的交集。因此,如果
a = [2, 3, 5, 6]
b = [17, 28, 2, 8, 0, 3]
我需要[2, 3]
。但是对于[3, 2, 5, 6]
和[17, 28, 2, 8, 0, 3]
我需要[2]
。
我怎样才能得到它?
解决方案
def inter(a,b):
c = []
for num in a:
if num in b and (not len(c) or b.index(c[-1]) < b.index(num)):
c.append(num)
return c
推荐阅读
- ios - 集合视图单元格中的iOS渐变颜色问题
- python - 如何使用哈希进行密码检查?
- python - 将值存储到init,但在读取新文件时它会不断覆盖列表
- sql-server - SQL Server 2008 R2 : restrict server login from seeing all but 2 databases on server
- vue.js - 无法在 ag-grid Vue 中读取未定义的属性“setQuickFilter”
- linux-kernel - 在同一个等待队列上等待不同的条件还是为每个条件使用单独的等待队列?
- android - 构建包含依赖 AAR 的 Android 库 AAR
- java - 创建一个实现 View.OnClickListener 的动态 onClick()
- c# - 如何从 libtiff.net TIFFCP.exe 捕获 C# 中的所有异常
- outlook-api - Outlook 休息 api ErrorNonExistentMailbox