Python collection type Usage Analysis, python collection usage
This article analyzes the usage of python set types. Share it with you for your reference. The specific analysis is as follows:
The collection type of python is similar to that of other languages. It is an unordered, non-repeating element set. I have never seen this type in other languages I have learned before, basic functions include link testing and deduplication. the Set object also supports mathematical operations such as union, intersection, difference, and sysmmetric difference, which are very similar to the set of Junior Mathematics.
1. First, let's take a look at the non-repetition of the python set type, which is very good for deduplication. For example, if we want to process some data, we want to give repeated data
Remove it, and then convert it to the set type in the operation, and then convert it from the set type to another type.
a = [2,3,4,2,1]
The final result we want to achieve is:
a = [1,2,3,4]
So how can we implement it.
Observe this list and we will find that there are repeated elements in the list, so we first think of removing the repeated elements in the list.
a = set(a)print a
The result of set a is:
set([1, 2, 3, 4])
The next step is to implement sorting. We think of a simple method, because the set does not have a sorting method, and the list has a sorting method, so we convert it to the python list type, call the sorting method of the list.
a = list(a)a.sort()print a
The result of list a is:
[1,2,3,4]
Speaking of collections, You can also talk about the summary of python tuples and python data types.
2. union, intersection, and difference)
a = set('abcde')b = set('bdcf')
Calculate the intersection of the Set:
A & B
The result is:
set(['c', 'b', 'd'])
Difference set:
A-B
The result is:
set(['a', 'e'])
Seek union:
A | B
The result is:
set(['a', 'c', 'b', 'e', 'd', 'f'])
Summary: The concept of a python set is similar to that of a mathematical set. It is often used for data deduplication and data transfer processing.
I hope this article will help you with Python programming.