在Python中两个没有循环的元组列表中找到普通元组的索引 [英] Find the indicies of common tuples in two list of tuples without loop in Python
本文介绍了在Python中两个没有循环的元组列表中找到普通元组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在两个元组列表中找到公共元组的索引?
How can one find the indexes of the common tuples in two list of tuples?
tuplelist1 = [("a","b"), ("c","d"), ("e","f"), ("g","h")]
tuplelist2 = [("c","d"),("e","f")]
因此tuplelist1中与tupplelist2相同的索引是索引1和2.
So the indices in tuplelist1 that are common with tupplelist2 are indices 1 and 2.
有没有办法解决这个问题?例如,是否可以通过集合或列表理解来做到这一点?
Is there a way to figure this out without a loop? Is there a way to do this with sets or list comprehension, for instance?
谢谢!
推荐答案
有了列表理解功能,您就可以做到
With a list comprehension, you could do
indices_of_shared = [index for (index, pair) in enumerate(tuplelist1) if pair in tuplelist2]
这篇关于在Python中两个没有循环的元组列表中找到普通元组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文