在 Python 中没有循环的两个元组列表中找到公共元组的索引
Find the indicies of common tuples in two list of tuples without loop in Python
如何在两个元组列表中找到公共元组的索引?
tuplelist1 = [("a","b"), ("c","d"), ("e","f"), ("g","h")]
tuplelist2 = [("c","d"),("e","f")]
因此tuplelist1中与tupplelist2共有的索引是索引1和2。
有没有办法不用循环就解决这个问题?例如,有没有办法通过集合或列表理解来做到这一点?
谢谢!
有了列表理解,你可以做到
indices_of_shared = [index for (index, pair) in enumerate(tuplelist1) if pair in tuplelist2]
如何在两个元组列表中找到公共元组的索引?
tuplelist1 = [("a","b"), ("c","d"), ("e","f"), ("g","h")]
tuplelist2 = [("c","d"),("e","f")]
因此tuplelist1中与tupplelist2共有的索引是索引1和2。
有没有办法不用循环就解决这个问题?例如,有没有办法通过集合或列表理解来做到这一点?
谢谢!
有了列表理解,你可以做到
indices_of_shared = [index for (index, pair) in enumerate(tuplelist1) if pair in tuplelist2]