Set

Set包含不重复元素,用{}或者set()创建,有于或非等运算符

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}

>>> print(basket)                      # show that duplicates have been removed

{'orange', 'banana', 'pear', 'apple'}

>>> 'orange' in basket                 # fast membership testing

True

>>> 'crabgrass' in basket

False



>>> # Demonstrate set operations on unique letters from two words

...

>>> a = set('abracadabra')

>>> b = set('alacazam')

>>> a                                  # unique letters in a

{'a', 'r', 'b', 'c', 'd'}

>>> a - b                              # letters in a but not in b

{'r', 'd', 'b'}

>>> a | b                              # letters in either a or b

{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}

>>> a & b                              # letters in both a and b

{'a', 'c'}

>>> a ^ b                              # letters in a or b but not both

{'r', 'd', 'b', 'm', 'z', 'l'}
>>> a = {x for x in 'abracadabra' if x not in 'abc'}

>>> a

{'r', 'd'}

 

你可能感兴趣的:(set)