Set and list are commonly used structure types in Python, and will not be described here. This article mainly summarizes some of the wonderful uses of them together.
(1) Deduplication
For example a sequence:
>>>line = ['a','b','a']
In order to remove the repeated 'a', the following can be done:
>>> list(set(line)) ['a', 'b']
(2) Extract the non-repeated elements that have appeared in the two sequences
For example two sequences:
>>> line1=['a','b','a'] >>> line2=['a','c']
To get the distinct elements that appear in these two sequences, do the following:
>>> line=line1+line2 >>> list(set(line)) ['a', 'c', 'b']
(3) Union of two sets
For example, the two sets are:
>>> set1=set(['a','b']) >>> set2=set(['a','c'])
To get the union, do the following:
>>> set(list(set1)+list(set2)) set(['a', 'c', 'b'])
(4) Calculate the Jacobian similarity of two sets
Jacobian similarity = intersection of two sets / union of two sets.
First, through the method in (3), the union of the two sets can be obtained, and then the number of elements in the union is 3;
Secondly, according to the number of elements in set1 is 2, the number of elements in set2 is 2, the number of the same elements in the two sets is 2+2-3=1 (verification is indeed 1, only 'a' is common element)
Finally, Jacobian similarity = 1/3.
(5) Determine whether a set is contained in another set
Based on (4), the number c of common elements of the two sets can be found. At this time, if c is equal to the total number of elements of a set, the set must be included in the other set; otherwise, it is not completely included.