Set of Python basic data types

Source: Internet
Author: User
Tags iterable new set set set


Set is a set of unordered and non-repeating elements, equivalent to a dictionary key, non-repeating, immutable

First, set Variable initialization

A = set () #注意在创建空集合的时候只能使用s =set () because s={} created an empty dictionary B = {"One", "one", "three", "four"}c = set (' boy ') D = set ([' Y ', ' b ', ' O ']) E  = Set ({"K1": ' v1 ', ' K2 ': ' V2 '}) F = {' K1 ', ' K2 ', ' k2 '}g = {(' K1 ', ' K2 ', ' K2 ')}print (A, type (a)) print (b, type (b)) print (C, Type (C)) print (d, type (d)) print (E, type (e)) print (F, type (f)) print (g, type (g)) Run Result: (set ([]), <type ' Set ' >) (Set ([ ' Four ', ' three ', ' one ', ' one ']), <type ' Set ' >) (set ([' Y ', ' b ', ' O ']), <type ' Set ' >) (set ([' Y ', ' b ', ' O ']), <t Ype ' Set ' >) (Set ([' K2 ', ' K1 ']), <type ' Set ' >) (Set ([' K2 ', ' K1 ']), <type ' Set ' >) (set ([' K1 ', ' K2 ', ' K2 ')]), <type ' Set ' >)

Second, set relationship

print e & f  # e  and  F  b of   print A | B  # A  and   print b -  A  #  differential Set (items in B, but not in A) print d ^ e  #  symmetric difference set (items in D or E, but not both)    Run Result: set ([' K2 ',  ' K1 ']) set ([' Four ',  ' one ',  ' three ',  ' "]) set ([' Four ',  ') One ',  ' three ',  ']) set ([' Y ',  ' K2 ',  ' K1 ' ",  ' B ',  ' O ']) 
x = set ([' I ',  ' e ',  ' m ',  ' d ',  ' t ']) y  = set (["I",  "D",  "E",  "a"]) print x.union (y)                  #  Set Set ([' E ',  ' d ',  ' I ',  ' h ',   ' j ',  ' m ',  ' t ']) print x.intersection (y)           #  intersection set ([' I ',  ' e ',  ' t ']) print x.difference (y)             #  difference Set Set ([' H ',  ' J ']) print x.symmetric_difference (y)  #  Symmetric differential operation result: Set ([' A ',  ' e ',  ' d ',  ' I ',  ' m ',  ' t ']) set ([' I ',  ' e ',  ' d ']) set ([' m ',   ' t ']) set ([' A ',  ' m ',  ' t ']) 
SE = set (["AA", "BB", "CC", "DD"]) ke = set (["AA", "BB"]) print se.difference (KE) # a exists, B does not exist, and a new sequence is generated SE.DIFFERENCE_UPDA Te (ke) # exists in, B does not exist, changes the original sequence print Seprint se.symmetric_difference (ke) # symmetric intersection, generates a new sequence se.symmetric_difference_update (KE) #对 Call intersection, update original sequence print SE run result: set ([' CC ', ' DD ']) set ([' CC ', ' DD ']) set ([' AA ', ' cc ', ' dd ', ' BB ']) set ([' AA ', ' BB ', ' cc ', ' DD '])

Third, the inclusion of the relationship

SE = set (["AA", "BB", "CC", "DD"]) ke = set (["AA", "BB"]) print Se.isdisjoint (KE) # Determine if two sets are not disjoint print se.issubset (KE) # judgment set is not included in the other collection, equivalent to A<=bprint se.issuperset (KE) # to determine whether the collection contains other collections, equivalent to the a>=b run result: falsefalsetrue

Iv. adding elements

SE = set (["AA", "BB", "CC"]) Ke = set ({"One", "X"}) Print Sese.add ("DD") # Add an element print sese.update (["DD", "EE"]) # Add multiple meta element print Sese.update (ke) # adds another collection of elements to print seset ([' AA ', ' cc ', ' BB ']) set ([' AA ', ' cc ', ' dd ', ' BB ']) set ([' AA ', ' BB ', ' cc ', ' DD ', ' ee ', ' DD ']) set ([' AA ', ' one ', ' + ', ' BB ', ' CC ', ' dd ', ' ee ', ' DD '])

V. Deleting elements

SE = set (["AA", "BB", "CC", "DD"]) se.discard ("AA") Print sese.remove ("BB") # element does not exist when an exception is thrown when print Sese.pop () # pops up a value that is random, cannot be specified Print SE Run results: set ([' CC ', ' dd ', ' BB ') ' Set ([' CC ', ' DD ']) set ([' DD '])

VI. Remove duplicate values

A = [one, one, one, one, one, one, 33]b = Set (a) print list (b) Run result: [33, 11, 44, 22, 55]

Vii. Common methods

Class set (mutableset[_t], generic[_t]):     def add (self, element:  _t)    #  Add an element     def clear (self)  #  empty collection      def copy (self)     def difference (self, *s: iterable[ Any])   # a exists in  b and returns a new set that can be assigned to other variables     def difference_update ( Self, *s: iterable[any])  # a exists in  b and changes the collection directly A    def discard (self, element: _t)  #   Delete single element     def intersection (self,  *s: iterable[any])     def intersection_update (self, *s: Iterable [Any])     def isdisjoint (Self, s: iterable[any])  #  determine if two sets are disjoint      def issubset (Self, s: iterable[any])  #  Determines whether a set is contained by another collection, equivalent to A<=b     dEf issuperset (Self, s: iterable[any])  #  Determines whether a collection contains other collections, equivalent to a>=b     def pop (self)  #  popup An element, random, not specified     def remove (self, element :  _t)  #  Remove a single element, if the element does not exist in the collection, it will error     def symmetric_difference (self, &NBSP;S:&NBSP;ITERABLE[_T])     def symmetric_difference_update (self, s:  ITERABLE[_T])     def union (self, *s: iterable[_t])      Def update (self, *s: iterable[_t])  #  Update individual elements, or other collections


This article is from the "Lifelong Learning" blog, please be sure to keep this source http://20120809.blog.51cto.com/10893237/1975068

Set of Python basic data types

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.