Python Learning: Collection (SET)

Source: Internet
Author: User


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)

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.