The example in this article analyzes Python collection type usage. Share to everyone for your reference. The specific analysis is as follows:
The Python collection type, like any other language, is a unordered set of elements that I have learned before in other languages that seem to have not seen this type, and the basic features include relational testing and the elimination of duplicate elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), difference (difference) and sysmmetric difference (symmetric difference set), which is very similar to our collection of mathematics in junior high school.
First, look at the Python collection type of duplication, this is done to do some processing is very good, for example, we have to deal with some data, to the duplication of data to
removed, and then in action, you can convert it to a collection type and then convert it to another type from the collection type.
The effect we will end up achieving is:
So how are we going to make it happen?
Looking at this list, we find that there are duplicate elements in the list, so our first thought is to get rid of the repeating elements in our lists.
The result of set A is:
The next step is to implement the sort, and we think of a simpler approach, because the collection has no sort method, and the list has a sort method, so we convert it to the type of Python list and call the sorting method of the list.
A = List (a)
a.sort ()
print a
The result of List A is:
When it comes to collections, it's also a summary of Python tuples and Python data types.
Union (Union), Intersection (AC), Difference (poor)
A = set (' ABCDE ')
B = Set (' BDCF ')
To find the intersection of a collection:
A & B
The result:
Set ([' C ', ' B ', ' d '])
To find the difference set:
A-b
The result:
Union:
a|b
The result:
Set ([' A ', ' C ', ' B ', ' e ', ' d ', ' F '])
Summary: Python sets and mathematical sets of concepts are more like, often used in the process of data reprocessing and some data transfer processing.
I hope this article will help you with your Python programming.