集合
- 集合(set)是一个无序的不重复元素序列;
- 可以使用大括号 { } 或者 set() 函数创建集合。
创建与添加
fruits = {'apple', 'orange'}
# 通过 set 去创建,入参是 可迭代对象对象
s1 = set('abcc')
print(s1) # {'b', 'a', 'c'}
s2 = set([1, 2, 3, 2])
print(s2) # {1, 2, 3}
s3 = set({ 'key1': 'a', 'key2': 'b'})
print(s3) # {'key1', 'key2'}
# add(x) --- 添加
fruits.add('pear')
print(fruits) # {'apple', 'pear', 'orange'}
# update(x) --- 添加多个,参数可以是列表,元组,字典等,
fruits.update(['banana', 'watermelon'])
print(fruits) # {'banana', 'pear', 'watermelon', 'apple', 'orange'}
计算集合元素个数
fruits = { 'apple', 'banana', 'cherry', 'berry', 'watermelon' }
print(len(fruits)) # 5
删除
fruits = { 'apple', 'banana', 'cherry', 'berry', 'watermelon' }
# remove -- 用于移除集合中的指定元素,但是在移除一个不存在的元素时会发生错误;
fruits.remove('apple')
print(fruits) # {'cherry', 'banana', 'watermelon', 'berry'}
fruits.remove('pear') # KeyError: 'pear'
# discard -- 同上,但是在移除一个不存在的元素时不会发生错误;
fruits.discard('banana')
print(fruits) # {'cherry', 'watermelon', 'berry'}
fruits.discard('pear')
print(fruits) # {'cherry', 'watermelon', 'berry'}
# pop -- 用于随机移除一个元素,并返回被移除的元素
print(fruits.pop()) # cherry
print(fruits) # {'watermelon', 'berry'}
# clear -- 清空集合
fruits.clear()
判断元素是否在集合中存在
判断一个元素是否在集合中,与其他类型没有什么区别,都是通过
in
关键字。
fruits = { 'apple', 'banana', 'cherry', 'berry', 'watermelon' }
print('apple' in fruits) # True
print('pear' in fruits) # False
两个集合之间的关系
两个集合是否为子集的关系,两个集合之间是否有相同元素以及合集,并集和差集。
判断
# isdisjoint -- 判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False
fruit_store = { "apple", "banana", "cherry", "tomato" }
greengrocer = { "eggplant", "cucumber", "tomato" }
print(fruit_store.isdisjoint(greengrocer)) # False
# issubset -- 方法用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False。
fruit_store = { "apple", "banana", "cherry", "tomato", "cucumber" }
greengrocer = { "cucumber", "tomato" }
print(greengrocer.issubset(fruit_store)) # True
# issuperset() 方法用于判断指定集合的所有元素是否都包含在原始的集合中,如果是则返回 True,否则返回 False。
fruit_store = { "apple", "banana", "cherry", "tomato", "cucumber" }
greengrocer = { "cucumber", "tomato" }
print(fruit_store.issuperset(greengrocer)) # True
并集(不改变原集合)
# union() 方法返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次。
fruit_store = { "apple", "banana", "cherry", "tomato" }
greengrocer = { "eggplant", "cucumber", "tomato" }
supermarket = fruit_store.union(greengrocer)
print(supermarket) # {'tomato', 'cucumber', 'apple', 'banana', 'eggplant', 'cherry'}
print(fruit_store) # {'tomato', 'apple', 'banana', 'cherry'}
print(greengrocer) # {'tomato', 'cucumber', 'eggplant'}
差集
difference
: 用于返回集合的差集,不修改原集合;difference_update
: 用于移除两个集合中都存在的元素。
fruit_store = { "apple", "banana", "cherry", "tomato" }
greengrocer = { "eggplant", "cucumber", "tomato" }
# difference --
print(fruit_store.difference(greengrocer)) # {'banana', 'cherry', 'apple'}
print(fruit_store) # {'banana', 'tomato', 'cherry', 'apple'}
print(greengrocer) # {'tomato', 'cucumber', 'eggplant'}
# difference_update
print(fruit_store.difference_update(greengrocer)) # None
print(fruit_store) # {'banana', 'cherry', 'apple'}
print(greengrocer) # {'tomato', 'cucumber', 'eggplant'}
交集
intersection
: 计算得出交集,不修改原集合;intersection_update
: 原集合上移除不重叠的元素,会修改原集合。
fruit_store = { "apple", "banana", "cherry", "tomato" }
greengrocer = { "eggplant", "cucumber", "tomato" }
# intersection() -- 方法用于返回两个或更多集合中都包含的元素,即交集。
print(fruit_store.intersection(greengrocer)) # {'tomato'}
print(fruit_store) # {'banana', 'tomato', 'cherry', 'apple'}
print(greengrocer) # {'tomato', 'cucumber', 'eggplant'}
# intersection_update() 原集合上移除不重叠的元素,会修改原集合。
print(fruit_store.intersection_update(greengrocer)) # None
print(fruit_store) # {'tomato'}
print(greengrocer) # {'tomato', 'cucumber', 'eggplant'}