Preface:
I have continuously translated the official python3.2 documents for a long time. I am so excited that I have been reading this article for the first time tonight. Thank you very much. I was a little excited when I saw everyone's support. So I also translated the set. Finally, I would like to thank you for your support.
3.4 Set
Python also contains a set of data types. A Set is an unordered Set that does not contain repeated elements. The basic application is to test the membership qualification and eliminate repeated elements. The Set object also provides arithmetic operators such as join, intersection, difference, and heap.
Brackets or the set () method can be used to create a set. Note: If you want to create an empty set, you must use set () instead {}. The latter creates an empty dictionary, which will be discussed in the next section.
Here is a simple example of using a set:
>>> Basket = {'apple', 'Orange ', 'apple', 'pear', 'Orange ', 'Banana '}
>>> Print (basket) # show that duplicates have been removed # show remove duplicate elements
{'Orange ', 'Banana', 'pear ', 'apple '}
>>> 'Orange 'in basket # fast membership testing # Quick member Detection
True
>>> 'Crabgrass 'in basket
False
>>># Display the unique letters obtained from the two words through the set operation
...
>>> A = set ('abracadaba ')
>>> B = set ('acazam ')
>>> A # unique letters in a contains a unique letter.
{'A', 'R', 'B', 'C', 'D '}
>>> A-B # letters in a but not in B is in a but not in B
{'R', 'D', 'B '}
>>> A | B # letters in either a or B in a or B
{'A', 'C', 'R', 'D', 'B', 'M', 'z', 'L '}
>>> A & B # letters in both a and B are in a and in B
{'A', 'C '}
>>> A ^ B # letters in a or B but not both IS in a or B, but not both.
{'R', 'D', 'B', 'M', 'z', 'L '}
Like for lists, there is a set comprehension syntax:
Like a list, a set also has "Understanding Syntax ".
>>> A = {x for x in 'abracadaba' if x not in 'abc '}
>>>
{'R', 'D '}