A set holds a unique group of data. In other words, even if you register the same data, it will be ignored. Enclose the set in {}. Or it can be expressed by set ([]).
set_explain1.py
set_a = {1,2,"a","b",1,"a"}
set_b = set([1,2,"a","b",1,"a"])
print("set_a {0}".format(set_a))
print("set_b {0}".format(set_b))
The output set contains only one 1 and "a". Also, since sets do not have a data order, they cannot be indexed like strings and lists.
Copy the following program and execute it. I will explain each function.
set_explain.py
set_a = {7,9,2}
set_b = {9,22,2,25}
set_b_25 = 25 in set_b
set_b_0 = 0 in set_b
print("set_b_25 {0}".format(set_b_25))
print("set_b_0 {0}".format(set_b_0))
print("len(set_a) {0}".format(len(set_a)))
set_b.add(12)
set_b.remove(25)
set_b.discard(0)
set_c = {"a","b","c"}
set_union = set_a.union(set_b)
print("set_union {0}".format(set_union))
set_unions = set_a.union(set_b,set_c)
print("set_unions {0}".format(set_unions))
set_intersection = set_a.intersection(set_b,set_c)
print("set_intersection {0}".format(set_intersection))
set_difference = set_a.difference(set_b)
print("set_difference {0}".format(set_difference))
set_differences = set_a.difference(set_b,set_c)
print("set_differences {0}".format(set_differences))
set_difference_1 = set_a.difference(set_b)
set_difference_2 = set_b.difference(set_a)
print("set_difference_1 {0}".format(set_difference_1))
print("set_difference_2 {0}".format(set_difference_2))
set_symmetric_difference = set_a.symmetric_difference(set_b)
print("set_symmetric_difference {0}".format(set_symmetric_difference))
set_big = {"a","b","c"}
set_small = {"a","b"}
set_issuper = set_big.issuperset(set_small)
set_issub = set_small.issubset(set_big)
print("set_issuper {0}".format(set_issuper))
print("set_issub {0}".format(set_issub))
Like a list, a set can also be ** x in set ** to see if an element exists in the set.
You can get the number of elements as well as the list. However, unlike the list, max () and min () do not exist in the set. Since indexing is not possible, it is not possible to replace by specifying the range.
It can be added with ** set.add (element) **. As mentioned earlier, even if you add an element that already exists There can never be more than one of the same element in a set.
It can be removed with ** set.remove (element) **. However, using remove () will result in an error if the element does not exist in the set. You can check with ** x in set ** in advance, but there is a convenient command called ** set.discard (element) **. This is a convenient feature that "deletes only when the element exists in the set".
The advantages of using sets are unions (mergers), intersections (intersections), and differences (difference sets). The point is that processing can be performed at high speed.
** Set 1. union (Set 2, Set 3,…) ** This is the process of removing duplicate elements from multiple sets to create a single set. The same process is possible with 3 or more sets.
** Set 1.intersection (Set 2, Set 3,…) ** Extracts elements that are duplicated in both (all) of the specified set. Similar to the union, the same processing can be performed from three or more elements.
** Set 1.difference (Set 2, Set 3,…) ** Extracts elements that exist on one side and not on the other side. Like the union, it can be processed from 3 or more elements.
In a normal difference, in X.difference (Y) and Y.difference (X) The results will be different (unless they are both in the same set). Check the executed program.
set_a set([9, 2, 7]) set_b set([2, 12, 22, 9]) set_difference_1 set([7]) set_difference_2 set([12, 22])
On the other hand, if you use a symmetric difference, you can use it regardless of the order. It is convenient to be able to extract the items that exist in one set of X and Y. ** Set 1.symmetric_difference (Set 2) ** Symmetric differences are limited to two sets.
In this way, sets are useful for determining duplicate values and handling joins.
Next: Python Basic Course (7 Dictionary)
Recommended Posts