This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
A set is an unordered sequence of elements that do not repeat
You can create a collection using curly braces {} or the set() function
To create an empty set, you must use set() instead of {}, because {} is used to create an empty dictionary
Collections and dictionaries are mutable types!! Previously, lists were mutable, strings and tuples immutable.
1. Getting to know the group
The advantages of Python collections
Since the elements in a set cannot appear more than once, this makes it largely efficient to remove duplicate values from a list or tuple, and to perform common mathematical operations such as union fetching and intersection fetching.
Hash types are collections and dictionaries
Note: Collections do not have index, slice, repeat, join properties, collections only member operators, iterated loop traversal, enumeration properties!
- Features: Mutable elements cannot be added to a set. (E.g., list)
A set is an unordered sequence of elements that does not repeat. You can create a collection using curly braces {} or the set() function. A = {1,2,3,45,12324} set(a)
Note: when creating an empty set, you can’t use a = {}. This is an empty dictionary. You should use set() instead.
Have intersection, union, difference set……
2. & common parts;
And sets: | all parts;
Difference set: – subtracts the shared part, keeping the value in the first original set.
(1) {}
Note: (Sets will automatically de-weight)
a = {'a','bb','yy','a','fg'}
print(a)
Copy the code
Output: {‘ a ‘, ‘bb’, ‘yy’, ‘fg’}
Extension: Quickly determine if an element is in a set. Use the in.
print('bb' in a)
Copy the code
If True, yes, False, no.
(2) set ()
a = set('whuhan')
b = set('zhangxinyi')
print(a)
Copy the code
The output is: {‘a’,’h’,’n’,’u’,’w’} unordered to repeat
Purpose: Set () can be used when deduplicating elements
2. Basic operations of the set:
- Note: Add must be added one at a time.
Collection: Unordered data types
The order of addition is not the same as the order of storage in the collection
a = {'a','bb','yy','fg'}
a.add('zhangxinyi')
print(a)
Copy the code
{‘zhangxinyi’,’yy’,’bb’,’a’,’fg’}
- Remove elements
(1)remove: delete
a = {'a','bb','yy','fg'}
a.remove('bb')
Copy the code
Output: {‘yy’,’a’,’fg’}
(2) Pop random deletion
a.pop()
Copy the code
Randomly deletes a value from the collection
-
Change the update
S = {1, 2, 3, 4, 5} s.u pdate (‘ wuhan)
Output: {‘w’,’1′,’a’,’2′,’h’,’3′,’u’,’5′,’n’,’4′}
- check
(1) Judge whether there is no intersection, if there is no intersection, the judgment is correct, otherwise error isdisjoint
S1 = {1,2,3} s2 = {3,4,5} s1.isdisjoint (s2)Copy the code
The output is False
(2) Determine whether set S1 is a subset of S2. If yes, output True; otherwise, output False
s1.issubset(s2)
Copy the code
The output is False
S1. issuperset(s2)
s1.issuperset(s2)
Copy the code
The output is False
Count the number of elements in the set
a = {'a','bb','yy','fg'}
b = set(a)
print(len(b))
Copy the code
The output is 4
A = [12,5,3,2,33], for example: if len(a)<=2:
-
Clear () clear()
a = {'a','bb','yy','fg'} a.clear() print(a) Copy the code
Output: set()
It’s important to remember that one major drawback to immutable sets is that since they’re immutable, this means you can’t add elements to them or remove elements from them!
2. Trimesh operations: (can optimize the code)
In computer languages, there is an expression called a triadic operation, also known as a ternary expression. Basic expression: variable = value 1 if condition else value 2 If condition is met, variable is valued at 1; Otherwise, the value is 2.
Example 1:
If a>b else b = a; if a>b else B = a; Otherwise, c is equal to b.Copy the code
Example 2:
a = 10
if a > 5:
print(True)
else:
print(False)
Copy the code
Convert to a trimesh operation expression:
print(True if a > 5 else False)
Copy the code
Print True if a > 5, False otherwise