[PYTHON] Methods available in set type

Methods available in set type

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

Methods available in set type
Methods available in the list
Set the form DateField to type = date in Django
Dynamically set env.hosts in fabric
Controlling request methods in django-rest-framework
String object methods in Python
Make Opencv available in Python
Dynamically call methods in Python
Set up Nunjucks in Node.js
Mastering type hinting in PyCharm
Set python test in jenkins
Save Time type in SQLAlchemy
Function argument type definition in python
Set opset to embed in ONNX
Dynamically load json type in python
Type specified in python. Throw exceptions
Dynamically define functions (methods) in Python