The role of the Python programming language is very obvious in practical applications, and its easy-to-use features are also very convenient for programmers. Today, we can take a look at the application characteristics of this language from the Python set type. The Python set type is an important application type, and sometimes it is quite useful. For detailed documentation and instructions, you can use help (set) to view the instructions and methods.
The following is a simple example.
- >>> x = set('spam')
- >>> y = set(['h','a','m'])
- >>> x, y
- (set(['a', 'p', 's', 'm']), set(['a', 'h', 'm']))
Let's look at some small Python set applications.
- >>> X & y # intersection
- Set (['A', 'M'])
- >>> X | y # Union
- Set (['A', 'P', 's', 'h', 'M'])
- >>> X-y # difference set
- Set (['P', 's'])
I remember a previous netizen asking how to remove repeated elements from the massive list and use hash to solve the problem. However, I felt that the performance was not very high and it was good to use set. The example is as follows:
- >>> a = [11,22,33,44,11,22]
- >>> b = set(a)
- >>> b
- set([33, 11, 44, 22])
- >>> c = [i for i in b]
- >>> c
- [33, 11, 44, 22]
It's cool, just a few lines. The above is the Python set type method we will introduce to you.