Python's set is similar to other languages and is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets).
Sets supports x in Set, Len (set), and for x in set. As an unordered collection, sets does not record the element position or insertion point. Therefore, sets does not support the operation of indexing, slicing, or other class sequences (Sequence-like).
>>>x = set (' spam ') >>>y = set (' Ham ') >>>x,yset ([' A ', ' P ', ' s ', ' M ']) set ([' A ', ' h ', ' m '])
Four basic operations for set (set):
>>>z = x | Y #并集set ([' A ', ' P ', ' s ', ' h ', ' m ']) >>>z = x & y #交集set ([' A ', ' m ']) >>>z = x-y #差集, the item is in X, no longer set in Y ([' P ', ' s ']) >>>
Python Learning: Collection (SET)