set
It is the basic data type of python to combine different elements to form a set
Properties of the collection:
- De duplication, a list programming set, will automatically de duplication
- Relation test: test the intersection, difference, union and other relations between two sets of data
- It doesn't need to mean there's no index, so you can't get a fixed value
- So this irrelevant data type is the way we read the contents: for loop or iterator
s = set('yuyang is DBA') s1 = ['yu','yang'] print(s) print(set(s1))
Execution result:
{'u', 'g', 'y', ' ', 'i', 'a', 'D', 'B', 'A', 'n', 's'} {'yang', 'yu'} Process finished with exit code 0
As can be seen from the above execution results, if there are duplicate characters in the set, the variable will only appear once when printing
-
A collection object is a set of hashable values that do not need to be arranged: collection members can be keys to a dictionary.
-
Hashable value, that is, an immutable type
a = ['yuyang is DBA',[1,2]] s = set(a) print(s)
ERROR:
Traceback (most recent call last): File "H:/python_scripts/study_scripts/daily/day14/set.py", line 2, in <module> s = set(a) TypeError: unhashable type: 'list' #This means that the list is a non hashable data type Process finished with exit code 1
If variable objects such as lists and dictionaries appear when assigning values to sets like the above, an error will be reported
Set classification:
- Variable set: elements can be added or deleted. Non hashable elements cannot be dictionary keys or other set elements
- Immutable set: opposite to variable set
Collection related operations
1. Create collection
s = set('yuyang is DBA') s1 = ['yu','yang'] print(s) print(set(s1))
Execution result:
{'u', 'g', 'y', ' ', 'i', 'a', 'D', 'B', 'A', 'n', 's'} {'yang', 'yu'} Process finished with exit code 0
2. Access collection
a = ['yuyang','is','DBA',1] s = set(a) print(s) for i in s: print(i) print('end'.center(30,'-')) print('yuyang' in s) print('jiayanping' in s)
Execution result:
{1, 'DBA', 'is', 'yuyang'} 1 DBA is yuyang -------------end-------------- True False Process finished with exit code 0
The collection is unordered, so if you need to access the value, you need to use for loop traversal, or iterator to implement
3. Add elements to collection
set.add()
a = ['yuyang','is','DBA',1] s = set(a) print(s) for i in s: print(i) print('end'.center(30,'-')) print('yuyang' in s) print('jiayanping' in s) s.add('jiayanping') print(s)
Execution result:
{1, 'is', 'yuyang', 'DBA'} 1 is yuyang DBA -------------end-------------- True False {1, 'is', 'yuyang', 'DBA', 'jiayanping'} Process finished with exit code 0
set.update()
a = ['yuyang','is','DBA',1] s = set(a) print(s) s.update('jiayanping') print(s)
Execution result:
{1, 'DBA', 'is', 'yuyang'} {1, 'DBA', 'yuyang', 'y', 'p', 'is', 'i', 'j', 'a', 'g', 'n'} Process finished with exit code 0
update only updates, and does not empty the previous content
The difference between add and update:
- add is to append the object as a whole
- update is to decompose objects into multiple elements in a sequence and append them to the collection one by one
Example:
a = ['yuyang','is','DBA',1] s = set(a) print(s) s.update([1,2,'jiayanping']) print(s)
Execution result:
{'yuyang', 'is', 1, 'DBA'} {1, 2, 'is', 'jiayanping', 'yuyang', 'DBA'} Process finished with exit code 0
4. Delete elements from collection
set.remove()
a = ['yuyang','is','DBA',1] s = set(a) print(s) s.remove('is') print(s)
Execution result:
{'DBA', 1, 'yuyang', 'is'} {'DBA', 1, 'yuyang'} Process finished with exit code 0
set.pop()
a = ['yuyang','is','DBA',1] s = set(a) print(s) s.pop() #Randomly delete an element print(s)
Execution result:
{'yuyang', 'DBA', 1, 'is'} {'DBA', 1, 'is'} Process finished with exit code 0
set.clear()
a = ['yuyang','is','DBA',1] s = set(a) print(s) s.clear() print(s)
Execution result:
{1, 'DBA', 'yuyang', 'is'} set() #This is an empty set Process finished with exit code 0
Set type operator
1.in and not in
2. Set is equivalent to nonequivalence (= =,! =)
print(set('yuyang') == set('yuyangyuyangyuyang'))
result:
True
Since the set will be de duplicated, the two quantities are equal here
3. Subset, superset
print(set('yuyang') < set('yuyang_jiayanping'))
result:
True
4. Joint
Relationship test
- intersection
- Difference set
- subset
a = set([1,2,3,4,5]) b = set([4,5,6]) print(a.union(b)) #Union print(a | b) #Union print(a.intersection(b)) #intersection print(a.difference(b)) #Difference set print(b.difference(a)) print(b.symmetric_difference(a)) #Symmetric difference set reverse intersection print(a-b) #Difference set print(b-a) #Difference set print(a^b) #Symmetric difference set reverse intersection print(b^a) #Symmetric difference set reverse intersection print(a&b) #intersection print(a.issuperset(b)) #Superset (parent set) a > b print(a.issubset(b)) #Subset a < B
result:
{1, 2, 3, 4, 5, 6} {1, 2, 3, 4, 5, 6} {4, 5} {1, 2, 3} {6} {1, 2, 3, 6} {1, 2, 3} {6} {1, 2, 3, 6} {1, 2, 3, 6} {4, 5} False False Process finished with exit code 0