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).
Example:
Print (' {} A word she can get what she {} for. ') Format (' With ', ' came ')) print (' {preposition} A word she can get to what she {verb} for. ') Format (preposition = ' with ', verb = ' came ')) print (' {0} A Worf she can get to what she {1} for. ') Format (' With ', ' came ')) S1 = set ([' LK ', ' Jim ', ' Tom ', ' Jim ') ' Print (S1) #打印会删除重复值s2 = s1.difference ([' Xzdz ', ' lk ']) print ( S2) #删除列表中的元素, the source file does not modify print (S1) #不变s1. difference_update ([' Xzdz ', ' lk ') print (S1) #源文件修改了s4 = S1.pop () print (S4) # The deleted element print (S1) #删除元素后x = set (' Szxpzm ') y = set ([' H ', ' Z ', ' m ', ' X ']) print (x&y) #交集print (x|y) #并集print (XY) #差集print (x , y)
Results:
With a word she can get how she came for. With a word she can get what she came for.with a Worf she can get to what she came for. {' Tom ', ' Jim ', ' LK '} {' Tom ', ' Jim '} {' Tom ', ' Jim ', ' LK '} {' Tom ', ' Jim '}tom{' Jim '} {' z ', ' x ', ' m '} {' x ', ' m ', ' Z ', ' s ', ' P ', ' h '} {' s ', ' P '} {' z ', ' x ', ' s ', ' m ', ' P '} {' z ', ' x ', ' m ', ' H '}
Python type set