Python Set (collection)

Source: Internet
Author: User

Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set. A set is an unordered, non-repeating combination of data, which has the following main functions: to go to the weight, to turn a list into a set, to automatically go back to the relationship test, to test the intersection of two sets of data, the difference set, and the set of relationships (set): The composition of different elements together is the basic data type of Python Collection elements (set elements): Members that make up the collection (non-repeatable) >>> li=[1,2, ' A ', ' B '] >>> s =set (LI) >>> print (s) # {1, 2, ' a ', ' B '}>>> li2=[1,2,1, ' A ', ' a ']>>> s=set (li2) >>> print (s) #{1, 2, ' a '} repeating elements are automatically filtered in set: #注意, The parameter passed in [1, 2, 3] is a list, and the displayed {1, 2, 3} just tells you that this set has 3 elements in the inside of a single, and that the order shown does not indicate that the set is ordered. A collection of related operations 1, create a collection because the collection does not have its own syntax format, can only be created through the collection of factory methods set () and Frozenset () >>> S1 = set (' Alvin ') >>> s2= frozenset (' Yuan ') >>> print (S1,type (S1)) #{' l ', ' V ', ' I ', ' a ', ' n '} <class ' Set ' >>>> print (S2,type (S2)) # Frozenset ({' n ', ' y ', ' a ', ' u '}) <class ' Frozenset ' >2, Access collection because the collection itself is unordered, you cannot create an index or slice operation for the collection, only iterate through or use in, not In to access or judge the collection element. >>> S1 = set (' Alvin ') >>> print (' A ' in S1) #True >>> print (' B ' in S1) #False >>> #s1 [1] # TypeError: ' Set ' object does not support INdexing>>> for I in s1:>>> print (i) #l i n v A3, update collection can be updated with the following built-in methods: S.add () s.update () Note only mutable collections can be updated:> >> #s1 = Frozenset (' Alvin ') >>> #print (S1) #frozenset ({' A ', ' n ', ' I ', ' V ', ' l '}) >>> #s1. Add ("CCDC ") #AttributeError: ' Frozenset ' object has no attribute ' add ' >>> s2 = set (' Alvin ') >>> s2.add (' mm ') >& gt;> print (s2) # {' mm ', ' l ', ' n ', ' a ', ' I ', ' V '}>>> s2.update (' HO ') # Add multiple elements >>> print (s2) # {' mm ', ' L ', ' n ', ' a ', ' I ', ' H ', ' O ', ' V '}4, delete collection s.remove () >>> s2 = set (' Alvin ') >>> >>> print (s2) # {' L ', ' n ', ' a ', ' I ', ' V '}>>> s2.remove (' l ') >>> print (s2) # {' n ', ' a ', ' I ', ' V '}>>> del S2 #删除集合本身 &G t;>> print (s2) #NameError: Name ' s2 ' is not defined four, set type operator 1 in, not in2 set equivalent to not equivalent (= =,! =) 3 subset, superset S=set (' Alvinyuan ') S1=set (' Alvin ') print (' V ' in s) print (S1&LT;S) 4 Union (|) Union (Union) operations are actually equivalent to the or operation of a set, and the Union symbol has an equivalent method, Union (). S1=set (' Alvin ') s2=set (' Yuan ') s3=s1|s2print (S3) #{' A ', ' l ', ' I ', ' n ', ' y ', ' V ', ' u '}print (s1.union (S2)) #{' A ', ' l ', ' I ', ' n ', ' y ', ' V ', ' u '} 5, Intersection (&) and the set and equivalence, the equivalent method of the intersection symbol is INTERSECTI On () S1=set (' Alvin ') s2=set (' Yuan ') s3=s1&s2print (S3) #{' n ', ' A '}print (s1.intersection (S2)) #{' n ', ' A '}6, check set (-) The equivalent method is Difference () s1=set (' Alvin ') s2=set (' Yuan ') s3=s1-s2print (S3) #{' V ', ' I ', ' l '}print (s1.difference (S2)) #{' V ', ' I ' , ' l '} 7, symmetric difference set (^) symmetric difference is the XOR (' XOR ') of the set, and the obtained elements belong to S1,S2 but do not belong to both S1 and S2. Its equivalent method symmetric_difference () S1=set (' Alvin ') S2=set (' Yuan ') S3=s1^s2print (S3) #{' l ', ' V ', ' y ', ' u ', ' I '}print (s1.symmetric_difference (S2)) #{' l ', ' V ', ' y ', ' u ', ' I '} application ' The simplest way to go heavy "lis = [1,2,3,4,1,2,3,4]print list (set (LIS)) #[1, 2, 3, 4]

Python set (collection)

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.