如何在两个元组列表中找到公共元组的索引?
tuplelist1 = [("a","b"), ("c","d"), ("e","f"), ("g","h")]
tuplelist2 = [("c","d"),("e","f")]
因此tuplelist1中与tupplelist2相同的索引是索引1和2.
有没有办法在没有循环的情况下解决这个问题?有没有办法用集合或列表理解来做到这一点,例如?
谢谢!
答案 0 :(得分:2)
使用列表理解,你可以做
indices_of_shared = [index for (index, pair) in enumerate(tuplelist1) if pair in tuplelist2]