Personal learning summary notes. If there are deficiencies and errors, please give advice. Thank you
Set
1. It consists of different elements, enclosed in {} braces and separated by commas
2. Disordered
3. Elements in the collection must be of variable type
4. The collection will be automatically de duplicated
For example: s = {1,2,3,4,5,6} # This is a collection
5. The set is variable, but it can be changed into an immutable set through the parameter frozenset
s = {1,'2',3,4,5,6} print(s) #{'2', 1, 3, 4, 5, 6} a = frozenset(s) #Immutable print(a) #frozenset({'2', 1, 3, 4, 5, 6})
1, Some common commands of collection
1. Set definition set
s = set('adam') print(s) #{'m', 'a', 'd'}
You can see that the output is out of order and has been de duplicated, and it has scattered the original string to the smallest unit
2. Add add element
s = {1,2,3,4,5,6} s.add('3') #Added a str type 3 print(s) #{1, 2, 3, 4, 5, 6, '3'} s.add(('4', 'a', 'b', 'c')) print(s) #{1, 2, 3, 4, 5, 6, ('4', 'a', 'b', 'c'), '3'}
1. If you add elements that are already contained in the collection, they will be automatically de duplicated
2. The added elements are placed out of order, that is, the set itself is out of order
3. If multiple elements are added, the collection will treat them as one element and package them. Only Yuanzu can insert them, but not lists and dictionaries
3. Clear clear
s = {1,2,3,4,5,6} s.clear() print(s) #set()
4. pop randomly deletes an element
s = {1,2,3,4,5,6,7} s.pop() print(s) #{2, 3, 4, 5, 6, 7}
5. remove specifies to delete
s = {1,2,3,4,5,6} s.remove(3) print(s) #{1, 2, 4, 5, 6}
PS: if no matching character is found, an error will be reported
6,discard Specify delete
s = {1,2,3,4,5,6} s.discard(7) print(s) #{1, 2, 3, 4, 5, 6}
PS: no error will be reported if no matching character is found to be deleted
2, Relational operation
1. Intersection Found the same element in two collections
s = {1,2,3,'m', 'a', 'd'} s1 ={1,'2',3,'m', 'd'} print(s&s1) #{1, 3, 'd', 'm'} print(s.intersection(s1)) #{1, 3, 'd', 'm'}
& Is an operation symbol, intersection It's an arithmetic command. Their results are the same
2. Merge two sets
s = {1,2,3,'m', 'a', 'd'} s1 ={1,'2',3,'m', 'd'} print(s|s1) #{1, 2, 3, 'm', '2', 'd', 'a'} print(s.union(s1)) #{1, 2, 3, 'm', '2', 'd', 'a'}
| Vertical is the operation symbol, union is the operation command, and their results are the same
3. The difference set retains the elements different from the former and removes the same elements
s = {1,2,3,'m', 'a', 'd'} s1 ={1,'2',3,'m', 'd'} print(s - s1) #{'a', 2} print(s1.difference(s)) #{'2'}
- The minus sign is the operation symbol, and the difference is the operation command. Their results are the same
Because the relationship between the two sets before and after the comparison is reversed, the results of the two outputs are also different
4. Cross complements retain only the different parts of the two
s = {1,2,3,'m', 'a', 'd'} s1 ={1,'2',3,'m', 'd'} print(s ^ s1) #{2, '2', 'a'} print(s1.symmetric_difference(s)) #{2, '2', 'a'}
^ It's an arithmetic symbol_ Difference is the operation command, and their results are the same
5. isdisjoint determines whether there is an intersection and returns a Boolean value
s = {1,2,3,'m', 'a', 'd'} s1 ={1,'2',3,'m', 'd'} s2 = {15,22} print(s1.isdisjoint(s2)) #True print(s1.isdisjoint(s)) #False
Returns True if there is no intersection and False if there is an intersection
6. issubset determines whether the latter contains the Boolean value returned by the former
s = {1,2,3,'m', 'a', 'd'} s1 ={1,2,3,'m', 'd'} s2 = {15,22,1} print(s1.issubset(s2)) #False print(s1.issubset(s)) #True
Returns True if the latter contains the former and False if the latter does not fully contain the former
7. issuperset determines whether the former completely contains the latter and returns a Boolean value
s = {1,2,3,'m', 'a', 'd'} s1 ={1,2,3,'m', 'd'} s2 = {15,22,1} s3 ={1,2,3,'m', 'a', 'd'} print(s.issuperset(s2)) #False print(s.issuperset(s1)) #True print(s.issuperset(s3)) #True
If the former completely contains the latter, it returns True. If the latter does not completely contain the latter, it returns False. It is also True if the two are equal
8. Update update
s = {1,2,3,'m', 'a', 'd'} s3 ={1,2,3,'m', 'a', 'd'} s1 ={1,2,3,'m', 'd'} s2 = {15,22,1} s.update(s3) print(s) #{1, 2, 3, 'a', 'm', 'd'} print(s3) #{1, 2, 3, 'd', 'm', 'a'} s1.update(s2) print(s1) #{1, 2, 3, 'm', 22, 'd', 15} print(s2) #{1, 22, 15}
Update the elements of the latter into the former
PS:
Access mode
Direct access: Digital
Sequential access: string, list, Yuanzu
keys: Dictionary
The dictionary has the fastest query speed, but the dictionary occupies a high memory
Number of stored elements:
Container class (storing multiple values): list, ancestor, dictionary
Atomic class (only one value can be stored): number, string