The collection of Python is not equal to the collection of other languages in a broad sense. Strictly speaking, a list is more like a collection, where set set is an unordered collection of non-repeating elements.
* Can be understood as, there is no value only the dictionary of keys. A dictionary of primitive form.
1 Creating a Collection
A_set = set () #空集合a_set = {-)
To create a collection with list:
A_set = Set ([Up, "ABC", 4])
2 Modifying a collection element
A_set.add (single Element) a_set.update (single or multiple elements or collections) #这里可以是集合, or it can be a repeating element in the list collection, and does not increase.
3 删除集合元素
A_set.discard (single Element) #删除不存在的元素, no effect A_set.remove (single element) #删除不存在的元素, throws Exception A_set.pop () A_set.clear ()
4 集合操作
A_set.union (B_set) #并集a_set. Intersection (B_set) #交集a_set. Difference (b_set) #差集a_set. Issubset (b_ Set) #检测子集
5 布尔检测
- Set () represents an empty collection, false
- Non-empty collection, True
Python 3 Grammar Note (4): Collection