Set of operations
Do you remember the concept of collection?
I remember the study of the previous "common logic" when the study is very clear, and now can find the previous notes it; and then self-learning "discrete mathematics" on the collection of various operations well aware of. However, after a lapse of many years, in addition to some very very basic concept, basically forgot to clean friends. First review together, in order to understand, I was 20 years ago, ancient books are turned out.
Common operations of collections:
- Intersection: A ∩ B = {x | x ∈ A and x ∈ B}
- And (Union): A ∪ B = {x | x ∈ A or x ∈ B}
- Supplement: A-B = {x | x ∈ A and x∉B}
Which complement can be called the difference set, but also divided into relative complement and absolute complement, but does not affect the things we want to discuss, so do not go into details, interested can be self-understanding.
Where to use
Since I said before that forget the clean, but also turned out Gansha ah? What is the use of the collection?
Quot; Baidu Encyclopedia entries in a passage;
The collection has an unparalleled special importance in the field of mathematics. The basis of set theory was established by the German mathematician Cantor in the 1870s, through a large number of outstanding scientists half a century of efforts to the 20th century, 20 years has established its modern mathematical theory system in the basic position, It can be said that almost all the branches of modern mathematics are built on rigorous set theory.
The amount, well, I look dizzy, I do not study mathematics, in fact, and STEEMIT related friends
Suppose I have some user accounts A, B, C, D, E, F
Then I can think of them as a collection of USERS
Namely: USERS = {A, B, C, D, E, F}
(Math representation, non-code)
Then, a post, @beststeem/test_1_2_3
Users A, B, C, H, I, J, give me this post a bit of praise, then these points praise, I can see as a collection VOTERS
Namely: VOTERS = {A, B, C, H, I, J}
(Math representation, non-code)
So the question, I would like to know who did not give me some praise, how to do it?
The previous approach is to use two lists, a list_user, a list_voter
And then loop through the elements in list_user to determine if they are in list_voter
Do not say the efficiency of the first, I feel not elegant. But fortunately can be used.
So I would like to be able to change the way at least look elegant?
Think about it, this is not the difference set Well (B: USERS; A: VOTERS)
Python support for collections
Fortunately, Python provides a very good support for collection operations
source: https://docs.python.org/2/library/sets.html#sets.Set
Which is the difference between: new set with elements in s but not in t, that is, we want the East friends
The operation is simpler than we thought
Code
Well, now to a specific code demo
Suppose I've got a list of voters for @ oflyhigh / test_1_2_3 this post
Then use this code to find who did not vote for me, hum 😕
list_user = ['oflyhigh', 'deanliu', 'ace108', 'laodr', 'rivalhw', 'lemoojiang']
list_voter = ['oflyhigh', 'ace108', 'laodr']
list_x = list(set(list_user) - set(list_voter))
print(list_x)
(The middle of the use of the list will be converted into a collection, you can also use the collection)
Ok, @deanliu, @rivalhw, @lemoojiang, do not give me @ oflyhigh / test_1_2_3 this post vote
I caught it out
In conclusion
In Python, you can use the set of difference set functions to find out who did not give me a chance to deal with some technical problems.
Reference link: