This article analyzes the Python collection type usage. Share to everyone for your reference. The specific analysis is as follows:
Python's collection type is similar to other languages and is an unordered set of distinct elements that I have not seen in the other languages I've learned before, and basic features include relationship testing and eliminating duplicate elements. The collection object also supports the mathematical Operations of Union (Union), intersection (intersection), Difference (poor) and sysmmetric difference (symmetric difference sets), and is very similar to the set of mathematics in our junior high school.
First, look at the Python collection type of non-repetition, this aspect of doing some deduplication is very good, for example, we have to deal with some data, want to put the duplicate data to
It can be converted to a collection type and then converted to other types by the collection type, if it is removed and then manipulated.
A = [2,3,4,2,1]
The final effect we will achieve is:
A = [1,2,3,4]
So how are we going to make it?
Looking at this list, we find that there are duplicate elements in the list, so the first thing we think about is to get rid of the repeating elements in the table.
A = set (a) print a
The result of collection A is:
Set ([1, 2, 3, 4])
The next step is to implement the sort, and we think of a simpler method, because the collection has no sorting method, and the list has a sort method, so we convert it to the type of the Python list, and call the sort method of the list.
A = List (a) A.sort () print a
The result of listing A is:
[1,2,3,4]
Speaking of collections, you can also talk about Python tuples and Python data types summary
II. Union (Union), intersection (cross), Difference (poor)
A = set (' ABCDE ') b = Set (' BDCF ')
To find the intersection of a collection:
A & B
The result is:
Set ([' C ', ' B ', ' d '])
Differential set:
A-B
The result is:
Set ([' A ', ' e '])
Seek Union:
A|b
The result is:
Set ([' A ', ' C ', ' B ', ' e ', ' d ', ' F '])
Summary: Python collections and mathematical collections of concepts are more like, often used in data de-processing and some data transfer processing.
Hopefully this article will help you with Python programming.