Python 有效地移除具有特定值的集合中的元组

Python 有效地移除具有特定值的集合中的元组,python,set,tuples,Python,Set,Tuples,我有一组元组,例如,set_of_tuples={(1,2},(1,3),(1,),(4,3)},我想删除所有包含例如a1的元组,我想知道有多少元组。如何有效地做到这一点 我的做法: set_of_tuples={(1,2),(1,3),(1,),(4,3)} to_be_removed = set() i=0 for val in set_of_tuples: if 1 in val: i=i+1 to_be_removed.add(val) set_o

我有一组元组,例如,set_of_tuples={(1,2},(1,3),(1,),(4,3)},我想删除所有包含例如a
1
的元组,我想知道有多少元组。如何有效地做到这一点

我的做法:

set_of_tuples={(1,2),(1,3),(1,),(4,3)}
to_be_removed = set()
i=0
for val in set_of_tuples:
    if 1 in val:
        i=i+1
        to_be_removed.add(val)
set_of_tuples = set_of_tuples-to_be_removed
我的代码在这个链接中 请看这个!!!:)


您是否尝试过
set_of u tuples={s for s in set_of u tuples,如果1不在s}
?如果您想知道删除了多少,请先保存集合的长度,然后减去结果的长度。不……非常感谢!