在Python中,可以使用&
运算符或intersection()
方法对两个集合进行交集运算。
例如,假设有两个集合A和B:
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
使用&
运算符进行交集运算:
intersection = A & B
print(intersection) # 输出:{4, 5}
使用intersection()
方法进行交集运算:
intersection = A.intersection(B)
print(intersection) # 输出:{4, 5}
注意,交集运算的结果仍然是一个集合。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python中replace的作用是什么