私信  •  关注

makis

makis 最近创建的主题
makis 最近回复了
5 年前
回复了 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] :返回索引列表

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