集合的运算


一、交集

1、intersection(获取集合a与集合b重复的元素)

a = {1,2,3,4}
b = {3,4,5,6}
c = a.intersection(b)
print(c)

2、intersection_update(删除集合a中与集合b不重复的元素)

a = {1,2,3,4}
b = {3,4,5,6}
a.intersection_update(b)
print(a)

二、并集(获取两个集合的元素后删除重复元素)

a = {1,2,3,4}
b = {3,4,5,6}
c = a.union(b)
print(c)

三、差集

1、symmetric_difference(获取集合a与集合b不重复的元素)

a = {1,2,3,4}
b = {3,4,5,6}
c = a.symmetric_difference(b)
print(c)

2、symmetric_difference_update(删除集合a中与集合b重复的元素)

a = {1,2,3,4}
b = {3,4,5,6}
a.symmetric_difference_update(b)
print(a)

3、difference(获取集合a中不与集合b重复的元素)

a = {1,2,3,4}
b = {3,4,5,6}
c = a.difference(b)
print(c)

4、difference_update(删除集合a中与集合b重复的元素)

a = {1,2,3,4}
b = {3,4,5,6}
a.difference_update(b)
print(a)

results matching ""

    No results matching ""