A set of mathematical meanings, without repeating elements, which must be hashed
1. Definition
s = Set ()
s = {A-i}
s = Set (can iterate over objects)
2, plus
S.add (Element) #增加1个元素, similar to a list of append
S.update (an iterative object) #增加一个可迭代对象, similar to a list of extend
3. Delete
S.remove (Element) #元素不存在, quote key error
S.pop () #随机删除并返回1个元素, when the collection is empty, report key error
S.discard (Element) #删除不存在元素的时候, no error
S.clear () #清空集合
4. A collection cannot modify a single element
5. Find
Collection cannot be indexed for lookup
Collection cannot access a single element
Collection No Lookup method
The collection is not a linear structure
The collection element has no order
6. Member operators: Determine if an element is in a container
In: Returns True
Not in: does not return true
When the data size is very large, the members of the collection are much more efficient than the list, and the scale is irrelevant; The list is related to its size, and the list is much faster when the scale is small.
7, set of operations
S1.intersection (S2) #交集
S1.intersection_update (S2) #交集并修改s1
S1 & S2 #求交集
S1.difference (S2) #差集
S1.difference_update (S2) #差集并修改s1
S1-s2 #差集
S1.symmetric_difference (S2) #对称差集
S1.symmetric_difference_update (S2) #对称差集并修改s1
S1 ^ S2 #对称差集, equivalent to XOR or
S1.union (S2) #并集
S1 | S2 #并集
8, the judgment of the set
S1.issubset (S2) #s1是否是s2的子集
S1.issuperset (S2) #s1是否是s2的超集
S1.isdisjoint (S2) #判断是否有交集, there is a false, inaction really
Python Learning 5-collection and collection operations