Python - 在元组集中定位元素

Raf*_*ues 2 python tuples set

我有一组元组,每个元组都有有限数量的元素.

我想检查该集合中是否存在某个元素,该元素具有我正在搜索的特定组件.

set_of_tuples = {(el_11, el_12, el_13), (el_21, el_22, el_23)}
a = (dont_care, el_12, dont_care)
Run Code Online (Sandbox Code Playgroud)

如何在集合中找到包含该特定组件的元组元素?

我可以使用列表理解来做到这一点,但这是一个非常缓慢的过程.

使用集合,在简单的情况下,我可以做这样的事情:

el = (1,2)
set_of_tuples = {(1,2), (2,3) ...}
Run Code Online (Sandbox Code Playgroud)

我可以验证它是否存在,在set_of_tuples中执行:el.

我问的是,有没有办法做同样的事情,但没有关心一些元组元素,例如:

el = (_,2)
el in set_of_tuples
Run Code Online (Sandbox Code Playgroud)

Rom*_*est 5

如果此元素存在于该集合的任何元组中

any()功能:

set_of_tuples = {(11, 12, 13), (21, 22, 23)}
el = 12
exists = any(el in t for t in set_of_tuples)

print(exists)    # True
Run Code Online (Sandbox Code Playgroud)

要获得外部集合中的位置:

pos = -1
for i,t in enumerate(set_of_tuples):
    if el in t:
        pos = i
        break

print(pos)
Run Code Online (Sandbox Code Playgroud)