社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  makis  »  全部回复
回复总数  1
7 年前
回复了 makis 创建的主题 » 比较不同列表python中两个元组的项

使用列表理解:

L1 = [('G', 'T'), ('T', 'T'), ('T', 'U'), ('U', 'I'), ('I', 'P')]
L2 = [('E', 'G'), ('G', 'T'), ('T', 'P')]

indices = [[L1.index(s),i] for i, s in enumerate(L2) if s in L1]

# print the first match (in this case there is only one match)
print(indices[0])
[0, 1]

解释 [[L1.index(s),i] for i, s in enumerate(L2) if s in L1] :

  • for i, s in enumerate(L2) :i是二级元组元素中的索引
  • if s in L1 :这将检查当前 s 也在L1
  • [L1.index(s),i] :返回索引列表

附言:对于复制品,这可能表现不好。