I. Introduction to the Collection
1. The collection is born to go heavy
2. The set is also unordered
3. The role of the set is mainly relationship testing, testing the intersection of two sets of data, difference set, set, subset, parent set, symmetric (inverse) difference set and other relationships
Second, the use of the collection method
A = {6,7,1,2,3,4,5} #创建一个集合
B=set ([1,2,3,4,8]) #创建一个集合
A & B #交集, which is present in a and B, running result: {1, 2, 3, 4}
A | B # Union, result of merge of A and B, operation result: {1, 2, 3, 4, 5, 6, 7, 8}
A-B #差集, A is not in a, run result: {5, 6, 7}
A ^ B # symmetric difference set, A and b not each other, running result: {5, 6, 7, 8}
A.add (' x ') #集合中添加一项
A.update ([888,999]) #集合中添加多项
A.remove (999) #删除一项, there will be an error
A.discard (999) #删除一项, there is no error
A.issubset (b) #a是否是b的子集
A.issuperset (b) #a是否是b的父集
Example (check if the password contains uppercase, lowercase letters, numbers, special characters)
All_nums = Set (String.digits)
Lower = set (String.ascii_lowercase)
Upper = set (String.ascii_uppercase)
Punctuation = Set (string.punctuation)
For I in range (5):
PWD = input (' Please enter your password: '). Strip ()
PWD = set (PWD)
If PWD & All_nums and PWD & Lower and PWD & Upper and PWD & punctuation:
Print (' password valid ')
Else
Print (' Password is not legal! ‘)
Collection of Python