python full stack development - Day5 collection

Keywords: Python Linux

python full stack development - Day5 collection

1, First of all, learn the set according to the following points

#1: Basic use

1. Purpose

2. Definition method

3. Common operation + built-in method

#2: Summary of this type

1. Save a value or multiple values. Only one value can save multiple values. What types of values can be

2. Order or disorder

3. Variable or immutable!!! Variable: the value changes and the id remains unchanged. Variable = = no hash!!! Immutable: when the value changes, the id changes. Immutable = = hashable

Two, set

#Functions: de duplication, relational operation,

#The variable type of knowledge point review is non hash type, the variable type is hash type

#Define set:

Set: can contain multiple elements, separated by commas,

The elements of a collection follow three principles:

1: each element must be immutable type (hash can be used as key of Dictionary)

2: no duplicate elements

3: Disorder

Note that the purpose of a set is to store different values together, and different sets are used for relational operation without tangled in a single value in the set

#Priority operations:

Length len

Member operations in and not in

3. Intersection

 

1 stus_linux={'alex','egon','Zhang Quan Chen','Li Tie Jie','oldboy'}
2 stus_python={'Li Er Ya','wxx','liudehua','alex','egon'}
3 
4 #Both enrolment linux Sign up again python Students of: intersection
5 print(stus_linux & stus_python)
6 print(stus_linux.intersection(stus_python))
7 
8 #  The values are all   {'alex', 'egon'}

Chen 4. Collection

 

1 stus_linux={'alex','egon','Zhang Quan Chen','Li Tie Jie','oldboy'}
2 stus_python={'Li Er Ya','wxx','liudehua','alex','egon'}
3 
4 #All students: Union
5 print(stus_linux | stus_python)
6 print(stus_linux.union(stus_python))
7 
8 #The values are all
9 #{'oldboy', 'egon', 'liudehua', 'Zhang Quan Chen', 'Li Er Ya', 'wxx', 'alex', 'Li Tie Jie'}

ා5, - difference set

 

 1 stus_linux={'alex','egon','Zhang Quan Chen','Li Tie Jie','oldboy'}
 2 stus_python={'Li Er Ya','wxx','liudehua','alex','egon'}
 3 
 4 #Only sign up linux,No entry python Difference sets
 5 print(stus_linux - stus_python)
 6 print(stus_linux.difference(stus_python))
 7 
 8 #The value is {'oldboy', 'Li Tie Jie', 'Zhang Quan Chen'}
 9 
10 
11 #Only sign up python,No entry linux Difference sets
12 print(stus_python -  stus_linux)
13 print(stus_python.difference(stus_linux))
14 
15 #The value is{'liudehua', 'Li Er Ya', 'wxx'}

Symmetric difference set

 

1 stus_linux={'alex','egon','Zhang Quan Chen','Li Tie Jie','oldboy'}
2 stus_python={'Li Er Ya','wxx','liudehua','alex','egon'}
3 
4 # Names of students who have not signed up for two courses at the same time: cross complement
5 print(stus_linux ^ stus_python)
6 print(stus_linux.symmetric_difference(stus_python))
7 
8 
9 #All values are {'Li Tie Jie', 'Li Er Ya', 'oldboy', 'liudehua', 'Zhang Quan Chen', 'wxx'}

 

Check

1 s1={1,'a','b','c','d'}
2 
3 for item in s1:
4     print(item)
5 
6 #The sorting of values is random, which is the random sorting of the above five elements

8. Increase

 1 s1={'a','b','c'}
 2 s1.add('d') # Add one value at a time
 3 s1.add(4)
 4 print(s1)
 5  
 6 #The value is{'a','b','c','d'}
 7 s1.update({3,4,5}) #Add multiple values at a time
 8 print(s1)
 9 
10 #The value is{'a','b','c','d',3,4,5}   Here, the order of the elements is random

9. Delete

 1 s1={'a','b','c'}
 2 #s1.discard() ## When the deleted element does not exist, no error will be reported
 3 s1.discard(4)
 4 print(s1)            #The value is{'a','b','c'}
 5 
 6 #s1.remove() # When the deleted element does not exist, an error is reported
 7 s1.remove(4)     #Report errors
 8 
 9 s1.pop() #Take an element at random
10 res=s1.pop()
11 print(res)       #Values are random

X 10, parent set, subset:

1 #Parent set:Father includes son
2 # s1={1,2,3}
3 # s2={1,2}
4 s1 > s2# representative s1 Contain s2,s1 yes s2 Parent set
5 s2 < s1 # s2 yes s1 subset

Posted by ericbangug on Sat, 04 Apr 2020 10:31:50 -0700