便宜VPS主机精选
提供服务器主机评测信息

python的set支持交集吗

是的,Python中的set支持交集操作。可以使用&运算符或者intersection()方法来计算两个集合的交集。例如:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# 使用 & 运算符计算交集
intersection = set1 & set2
print(intersection)  # 输出 {3, 4}

# 使用 intersection() 方法计算交集
intersection = set1.intersection(set2)
print(intersection)  # 输出 {3, 4}

未经允许不得转载:便宜VPS测评 » python的set支持交集吗