Collection set in Python

Source: Internet
Author: User
Tags set set

The set in Python 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 supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets).

Set supports X in Set, Len (set), and for X in set operations. As an unordered collection, set does not record element positions or insertion points, so indexing, slicing, and so on, are not supported for the operations of class sequences (Sequence-like).

One of the most important features of a collection is deduplication, which eliminates duplicate elements. The elements in the list can be duplicated, and if you want to remove duplicate elements from a list, simply convert the list to a collection:

>>> a = [1,4,9,16,25,36]>>> a.insert (bis) >>> a[1, 4, 9, 4, 36]>>> B = Set (a) &G t;>> bset ([1, 36, 9, 16, 25, 4])

Find the intersection, the set, and the difference set of two sets:

>>> c = set ([16,25,49,64,81,100]) >>> bset ([1, 36, 9, 16,  25, 4]) >>> cset ([64, 100, 49, 16, 81, 25]) >>>  B.intersection (c)             #  intersection set ([16,  25]) >>> b & c                         #  intersection set ([16, 25] ) >>> b.union (c)                     #  Set Set ([64, 1, 36, 100, 81, 9, 16,  49, 25, 4]) >>> b | c                         #  Set Set ([ 64, 1, 36, 100, 81, 9, 16, 49, 25, 4]) >>> b.difference (c)                #  difference Set (element in B is not in c) set ([1, 36, 4,  9]) >>> b - c                         #  difference Set (element in B is not in c) set ([1,  36, 4, 9]) >>> c.difference (b)                #  difference Set (element in C is not in B) set ([64, 49, 100, 81]) >>>  c - b                         #  difference Set (element in C is not in B) Set ([64, 49, 100,  81]) >>> b.symmetric_difference (c)     #  symmetric difference set (the element appears in two sets when different) set ([64 ,  49, 100, 1, 81,  4, 9, 36]) >>> b ^ c                         #  Symmetric difference Set ( Elements appear in two sets when different) set ([64, 49, 100, 1, 81, 4, 9, 36]) >>>  B.difference_update (c)        #  update B>>> bset with an element that is not in C only in B ([ 1, 36, 9, 4]) >>> cset ([64, 100, 49, 16, 81, 25])

To add an element to the collection:

>>> bset ([1, 9, 4]) >>> B.add (16) # add element to B 16>>> B.add (25) # add element 25 to B >>> bset ([1, 9, 4] >>> b.update (c) # adds all elements in C (not including elements in B) to B >>> bset ([64 , 1, 36, 100, 81, 9, 16, 49, 25, 4])

Include relationship:

True>>> 121 not in ctrue>>> C.issubset (b) # C is a subset of B true>>> B.issuperset (c) # B is a superset of C t Rue>>> B > Ctrue

To remove an element from the collection:

>>> bset ([4, 1, 9,, B.remove, 121) # >>> The removal of elements that do not belong to the collection with remove error trace           Back (most recent): File ' <stdin> ', line 1, <module>KeyError:121>>> B.discard (121) # Nothing happens when you delete an element that does not belong to the collection with discard >>> bset ([1, 4, 9, BA, +, +, (+), (+), [+] (+)] >>> B.discard ;>> for M in (+, Bayi, +): B.discard (m) ... >>> bset ([1, 36, 9, 16, 25, 4])


Collection set in Python

Related Article

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.