1. Definition
A collection is an unordered collection of objects, surrounded by braces { }, and separated by commas
Collections are dynamic , elements can be added or removed at any time
Collections are heterogeneous and can contain different types of data
The only element in the set
Two, create
- Created by curly braces
- Created by the constructor set()
- Created via set comprehension
# 大括号创建
st1 = {1, "2", True}
print(st1) # 输出:{1, "2"} 因为元素唯一,1和True重复
# 构造方法set()创建
st2 = set()
print(st2) # 输出:set()
st2 = set("good") # 字符串、元组、列表、集合、字典等均可
print(st2) # 输出:{'d', 'o', 'g'}
# 集合推导式创建
st3 = {i for i in range(0,4)}
print(st3) # 输出:{0, 1, 2, 3}
# 注意:不要单独使用{}创建空集合
st4 = {}
print(st4) # 输出:{}
print(type(st4)) # 输出:<class 'dict'>
3. Collective use
member detection
in: Checks whether an element is in the set. If it returns True, otherwise it returns False.
not in: Checks whether a collection does not contain an element. Return True if not, otherwise return False.
st = {i for i in range(1,4)}
print(1 in st) # 输出:True
print(100 not in st) # 输出:True
4. Common methods
1、add()
Add a single object to a collection
Writing method: set.add(item)
Input parameter: object item
return: None
st = {0, 1, 2, 3}
st.add(False)
st.add('good')
print(st.add(99)) # 输出:None
print(st) # 输出:{0, 1, 2, 3, 99, 'good'}
2、update()
Adds all elements of an iterable to the collection
Writing: set.update(iterable)
Input parameter: iterable object iterable
return: None
st = {1}
st.update(('a', 'b', 'c'))
print(st.update("good")) # 输出:None
print(st) # 输出:{1, 'a', 'b', 'o', 'c', 'd', 'g'}
3、remove()
Remove the specified element item from the collection
Writing: set.remove(item)
Input parameter: specified element item
return: None
The target element must already exist, otherwise a KeyError will be reported
st = {1, 2, 3}
print(st.remove(2)) # 输出:None
print(st) # 输出:{1, 3}
st.remove(99)
print(st) # KeyError: 99
4、discard()
Remove the specified element item from the collection
Writing: set.discard(item)
Input parameter: specified element item
return: None
No error will be reported if the target element does not exist
st = {1, 2, 3}
print(st.discard(2)) # 输出:None
print(st) # 输出:{1, 3}
st.discard(99)
print(st) # 输出:{1, 3}
5、pop()
Randomly removes an element from a collection
Writing method: set.pop()
Entry parameters: none
Returns: the removed element
Raises a KeyError if the collection is empty
st = {1, 2}
st.pop()
print(st) # 输出:{2} 或者 {1}
print(st.pop()) # 输出:被移除的 2 或者 1
print(st) # 输出:set()
st.pop() # KeyError: 'pop from an empty set'
6、clear()
Empty the collection, removing all elements
Entry parameters: none
return: None
st = {1, 2, 3, 'a', 'b'}
print(st.clear()) # 输出:None
print(st) # 输出:set()
5. Set operations
1. Intersection operation
Method: intersection()
Operator: &
Writing: set1.intersection(set2), set1 & set2
st1 = {1, 3, 2}
st2 = {5, 1, 4}
print(st1.intersection(st2)) # 输出:{1}
print(st1 & st2) # 输出:{1}
2. Union operation
Method: union()
Operator: |
Writing: set1.union(set2), set1 | set2
st1 = {1, 3, 2}
st2 = {5, 1, 4}
print(st1.union(st2)) # 输出:{1, 2, 3, 4, 5}
print(st1 | st2) # 输出:{1, 2, 3, 4, 5}
3. Difference operation
Method: difference()
Operator: -
Writing: set1.difference(set2), set1 - set2
st1 = {1, 3, 2}
st2 = {5, 1, 4}
print(st1.difference(st2)) # 输出:{2, 3}
print(st1 - st2) # 输出:{2, 3}
Six, collection derivation
Set comprehension refers to creating collections in a loop, similar to list comprehension
Writing: [x for x in ... if ...]
# for循环创建集合
st1 = set()
for i in 'hello world':
if i in 'good':
st1.add(i)
print(st1) # 输出:{'d', 'o'}
# 集合推导式创建集合
st2 = {i for i in 'hello world' if i in 'good'}
print(st2) # 输出:{'d', 'o'}