首页 > 解决方案 > 有效地删除具有特定值的集合中的元组

问题描述

我有一组元组,例如 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

标签: pythonsettuples

解决方案


我的代码在这个链接中,请看这个!!!:)

https://onlinegdb.com/HJ8RX2LNO


推荐阅读