Python3.2 official document tutorial --- Set

Source: Internet
Author: User

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 '}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.