python-Collection (SET)

Source: Internet
Author: User

                                    Collection

A collection is an unordered, non-repeating collection of data in which the elements are hashed (immutable types), but the collection itself is not hashed (so the collection cannot be a key to the dictionary). The following are the two most important points of the collection:
Go to the weight, turn a list into a set, and automatically go heavy.
Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set.

# The elements inside the collection are hashed
# Set1 = {' 1 ', ' Alex ', 2,true,[1,2,3]}  #报错 #  set2 = {' 1 ', ' Alex ', 2,true,{1:2}}  #报错  #  set3 = {' 1 ', ' Alex ', 2,true, (1,2,[2,3,4])}  #报错
Go heavy
# l= [1,1,2,2,3,4,5,5] # Set1 = set (l) # Print (Set1,type (SET1)) # L = List (Set1) # Print (L,type (l))
Creation of collections
# Set1 = Set ({A-st, ' Barry '}) # Set2 = {, ' Barry '} # Print (Set1,type (SET1)) # Print (Set2,type (Set2))
Increase
# Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '} # set1.add (' Wen ') # print (Set1) # Update: Iterate increments # set1.update (' A ') # print (Set1) # set1.update (' teacher ') # print (Set1) # set1.update ([i]) # print (Set1)

By deleting
#set = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}#set.remove (' Alex ') #删除一个元素#print (set)#Random Pop deletion#Set.pop () #随机删除一个元素#print (set)#Clear Clear#set.clear () #清空集合#print (set)#del#del set #删除集合#print (set)
Other operations of the collection
#1. Intersection (& or intersection)#Set1 = {1,2,3,4,5}#Set2 = {4,5,6,7,8}#print (Set1&set2) #-->{4, 5}#Print (Set1.intersection (Set2))#2. Union (| or unions)#Set1 = {1,2,3,4,5}#Set2 = {4,5,6,7,8}#Print (Set1 | set2) #-->{1, 2, 3, 4, 5, 6, 7, 8}#Print (Set1.union (Set2)) #-->{1, 2, 3, 4, 5, 6, 7, 8}#3. Difference set (-or difference)#Set1 = {1,2,3,4,5}#Set2 = {4,5,6,7,8}#print (Set1-set2)#Print (Set1.difference (Set2)) #-->{1, 2, 3}#4. Inverse intersection (^ or symmetric_difference)#Set1 = {1,2,3,4,5}#Set2 = {4,5,6,7,8}#Print (set1 ^ set2) #-->{1, 2, 3, 6, 7, 8}#Print (Set1.symmetric_difference (Set2))#5 subsets and hyper-sets#Set1 = { A-i}#Set2 = {1,2,3,4,5,6}#Print (Set1 < Set2)#Print (Set1.issubset (Set2)) # These two are the same, both of which illustrate that Set1 is a subset of Set2#print (Set2 > Set1)#Print (Set2.issuperset (SET1)) # These two are the same, both of which illustrate that Set2 is a set1 superset. 
Frozenset the immutable collection, making the collection immutable.
# s = Frozenset (' Barry ') # Print (S,type (s))  # Frozenset ({' A ', ' Y ', ' B ', ' R '}) <class ' Frozenset ' >

Shallow copy
 #   L1 = [1,2,3,[' Barry ', ' Alex ']]  #   L2 = L1  ##  Span style= "COLOR: #008000" > l1[0] = 111  #   Print (L1) # [111, 2, 3, [' Barry ', ' Alex ']]  #   Print (L2) # [111, 2, 3, [' Barry ', ' Alex ']]  #   L1[3][0] = ' Wusir '  #   print (L1) # [111, 2, 3, [' Wusir ', ' Alex ']]  #   print (L2) # [111, 2, 3, [' Wusir ', ' Alex ']]  #   print (ID (L1), id (L2)) #- --for assignment operations, L1 and L2 point to the same memory address, so they are exactly the same. 
# li = [1,2,3,[' Barry ', ' Alex ']
# L2 = Li.copy ()
# Print (Li,id (LI))
# Print (L2,id (L2))
# Li[1] = 222
# Print (Li,id (LI))
# Print (L2,id (L2))
# Li[3][0] = ' Wusir '
# Print (Li,id (li[3))
# Print (L2,id (l2[3))
# for shallow copy, the first layer creates a new memory address, and starting at the second level, it points to the same memory address.
# So, for the second and deeper layers, maintain consistency

# deep Copy Deepcopy
#Import Copy#li = [1,2,3,[' Barry ', ' Alex ']#L2 = Copy.deepcopy (li)#print (Li,id (LI))#Print (L2,id (L2))##li[1] = 222#print (Li,id (LI))#Print (L2,id (L2))##li[3][0] = ' Wusir '#Print (Li,id (li[3) )#Print (L2,id (l2[3) )#for deep Copy, the two are completely antagonistic, changing any element of any one (no matter how many layers), and the other absolutely does not change

python-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.