In the example below, I used the numbers above, but it works for strings as well.
It is a method that returns a unique union, and can be used in the same way as the operator "|".
a1 = {1, 2, 3} a2 = {2, 3, 4} a3 = {aa, bb, cc} a3 = {bb, cc, dd}
union_a = a1.union(a2) print(union_a) # {0, 1, 2, 3}
The result is as above. Since the union () method returns a unique union, "2,3" will not be returned in duplicate.
Returns a duplicate aggregate. It can be used in the same way as the operator "&".
a1 = {1, 2, 3} a2 = {2, 3, 4}
intersection_a = a1.intersection(a2) print(intersection_a) # {2, 3}
The result is as above. The intersection () method returns an overlapping set, so "2,3" is returned.
Returns the set of differences. It can be used in the same way as the operator "-". The following example returns something that exists in "a1" but does not exist in "a2".
a1 = {1, 2, 3} a2 = {2, 3, 4}
difference_a = a1.difference(a2) print(difference_a) # {1}
"4" does not return because it exists only in "a2".
Returns the contrast difference. It can be used in the same way as the operator "^".
a1 = {1, 2, 3} a2 = {2, 3, 4}
symmetric_difference_a = s1.symmetric_difference(s2) print(symmetric_difference_a ) # {1, 4}
The non-overlapping parts of "a1" and "a2" are returned.
Arguments are added to the value of set.
a= {1, 2, 3} a.add(4) print(a) # {1, 2, 3, 4}
s = set() s.add(aa) s.add(bb) s.add(cc) print(s) # {aa, bb, cc}
Remove the argument. If the argument passed as the element to be deleted is not registered, "KeyError" will occur.
a= {1, 2, 3}
a.remove(3) print(a) # {1, 2}
Recommended Posts