|
1 | 1 | Sets |
2 | 2 | ==== |
3 | 3 |
|
4 | | -Sets in Python are an unordered collection of objects that are used in |
5 | | -situations where membership and uniqueness to the set are the most important |
6 | | -information of the object. The ``in`` operator runs faster with sets than with |
7 | | -:doc:`lists`: |
| 4 | +Sets in Python are an unordered collection of objects used in situations where |
| 5 | +membership and uniqueness to the set are the most important information of the |
| 6 | +object. The ``in`` operator runs faster with sets than with :doc:`lists`: |
8 | 7 |
|
9 | 8 | .. _set: |
10 | 9 |
|
11 | 10 | ``set`` |
12 | 11 | ------- |
13 | 12 |
|
| 13 | +Create sets |
| 14 | +~~~~~~~~~~~ |
| 15 | + |
| 16 | +You can create sets by applying :class:`set` to a sequence, for example to a |
| 17 | +:doc:`list <lists>`. |
| 18 | + |
14 | 19 | .. code-block:: pycon |
15 | | - :linenos: |
16 | 20 |
|
17 | | - >>> x = set([1, 2, 3, 2, 4]) |
18 | | - >>> x |
19 | | - {1, 2, 3, 4} |
20 | | - >>> 1 in x |
| 21 | + >>> sequences = set(["list", "tuple", "tuple"]) |
| 22 | + >>> sequences |
| 23 | + {'tuple', 'list'} |
| 24 | +
|
| 25 | +If a sequence is made into a set, duplicates are removed, but the order is then |
| 26 | +also lost. |
| 27 | + |
| 28 | +Individual elements cannot be selected with slicing either: |
| 29 | + |
| 30 | +.. code-block:: pycon |
| 31 | +
|
| 32 | + >>> sequences[0] |
| 33 | + Traceback (most recent call last): |
| 34 | + File "<python-input-27>", line 1, in <module> |
| 35 | + sequences[0] |
| 36 | + ~~~~~~~~~^^^ |
| 37 | + TypeError: 'set' object is not subscriptable |
| 38 | +
|
| 39 | +Check values |
| 40 | +~~~~~~~~~~~~ |
| 41 | + |
| 42 | +The keyword ``in`` is used to check whether an object belongs to a set. |
| 43 | + |
| 44 | +.. code-block:: pycon |
| 45 | +
|
| 46 | + >>> "list" in sequences |
21 | 47 | True |
22 | | - >>> 5 in x |
| 48 | + >>> "set" in sequences |
23 | 49 | False |
24 | | - >>> x.add(0) |
25 | | - >>> x |
26 | | - {0, 1, 2, 3, 4} |
27 | | - >>> x.remove(4) |
28 | | - >>> x |
29 | | - {0, 1, 2, 3} |
30 | | - >>> y = set([3, 4, 5]) |
31 | | - >>> x | y |
32 | | - {0, 1, 2, 3, 4, 5} |
33 | | - >>> x & y |
34 | | - {3} |
35 | | - >>> x ^ y |
36 | | - {0, 1, 2, 4, 5} |
37 | | - >>> x.update(y) |
38 | | - >>> x |
39 | | - {0, 1, 2, 3, 4, 5} |
40 | | -
|
41 | | -Line 1 |
42 | | - You can create a set by applying ``set`` to a sequence, for example to a |
43 | | - :doc:`list <lists>`. |
44 | | -Line 3 |
45 | | - When a sequence is made into a set, duplicates are removed. |
46 | | -Lines 4–7 |
47 | | - The keyword ``in`` is used to check whether an object belongs to a set. |
48 | | -Lines 8–13 |
49 | | - With ``add`` and ``remove`` you can change the elements in set. |
50 | | -Line 15 |
51 | | - ``|`` is used to get the union or combination of two sets. |
52 | | -Line 17 |
53 | | - ``&`` is used to get the intersection. |
54 | | -Line 19 |
55 | | - ``^`` is used to find the symmetrical difference, meaning elements that are |
56 | | - contained in one or the other set, but not in both. |
| 50 | +
|
| 51 | +Add and delete values |
| 52 | +~~~~~~~~~~~~~~~~~~~~~ |
| 53 | + |
| 54 | +You can add and delete values with ``add`` and ``remove``. |
| 55 | + |
| 56 | +.. code-block:: pycon |
| 57 | +
|
| 58 | + >>> quantities = sequences.add("set") |
| 59 | + >>> quantities |
| 60 | + {'list', 'tuple', 'set'} |
| 61 | + >>> quantities.remove("set") |
| 62 | + >>> quantities |
| 63 | + {'list', 'tuple'} |
| 64 | +
|
| 65 | +The elements are unordered, which means that the values within a sequence can |
| 66 | +shift when new elements are added. |
| 67 | + |
| 68 | +Set formation |
| 69 | +~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +Union set |
| 72 | + .. code-block:: pycon |
| 73 | +
|
| 74 | + x = {4, 2, 3, 2, 1} |
| 75 | + y = {3, 4, 5} |
| 76 | + >>> x.union(y) |
| 77 | + {0, 1, 2, 3, 4, 5} |
| 78 | +
|
| 79 | +Intersection |
| 80 | + .. code-block:: pycon |
| 81 | +
|
| 82 | + >>> x.intersection(y) |
| 83 | + {3} |
| 84 | +
|
| 85 | +Difference or remainder set |
| 86 | + |
| 87 | + .. code-block:: pycon |
| 88 | +
|
| 89 | + >>> x.difference(y) |
| 90 | + {0, 1, 2} |
57 | 91 |
|
58 | 92 | .. _frozenset: |
59 | 93 |
|
60 | 94 | ``frozenset`` |
61 | 95 | ------------- |
62 | 96 |
|
63 | | -In addition to ``set``, there is also ``frozenset``, which is immutable. This |
| 97 | +In addition to ``set``, there is also ``frozenset``, an immutable data type. This |
64 | 98 | means that they can also be members of other sets: |
65 | 99 |
|
66 | 100 | .. code-block:: pycon |
67 | 101 | :linenos: |
68 | 102 |
|
69 | | - >>> x = set([4, 2, 3, 2, 1]) |
70 | | - >>> z = frozenset(x) |
71 | | - >>> z |
72 | | - frozenset({1, 2, 3, 4}) |
73 | | - >>> z.add(5) |
| 103 | + >>> sequences = frozenset(["list", "tuple", "set", "tuple"]) |
| 104 | + >>> sequences |
| 105 | + frozenset({'list', 'tuple', 'set'}) |
| 106 | + >>> dicts = {"dict"} |
| 107 | + >>> sequences.add(dicts) |
74 | 108 | Traceback (most recent call last): |
75 | | - File "<stdin>", line 1, in <module> |
| 109 | + File "<python-input-18>", line 1, in <module> |
| 110 | + sequences.add(dicts) |
| 111 | + ^^^^^^^^^^^^^ |
76 | 112 | AttributeError: 'frozenset' object has no attribute 'add' |
77 | | - >>> x.add(z) |
78 | | - >>> x |
79 | | - {1, 2, 3, 4, frozenset({1, 2, 3, 4})} |
| 113 | + >>> dicts.add(sequences) |
| 114 | + >>> dicts |
| 115 | + {frozenset({'list', 'tuple', 'set'}), 'dict'} |
| 116 | +
|
| 117 | +Performance |
| 118 | +----------- |
| 119 | + |
| 120 | +Sets are very fast when checking whether elements are contained in a set. The set |
| 121 | +arithmetic of sets is also well suited to finding common and unique values of two |
| 122 | +sets. For this purpose, it can be useful to convert :doc:`lists` or :doc:`tuples` |
| 123 | +into sets. |
80 | 124 |
|
81 | 125 | Order |
82 | 126 | ----- |
83 | 127 |
|
84 | | -However, the speed advantage also comes at a price: sets do not keep the |
85 | | -elements in the correct order, whereas :doc:`lists` and :doc:`tuples` do. If the |
86 | | -order is important to you, you should use a data structure that remembers the |
87 | | -order. |
| 128 | +However, the speed advantage also comes at a price: sets do not keep the elements |
| 129 | +in the correct order, whereas :doc:`lists` and :doc:`tuples` do. If the order is |
| 130 | +important to you, you should only convert the elements into a set for certain |
| 131 | +operations, for example to check whether the elements of a list are unique with |
| 132 | + |
| 133 | +.. code-block:: pycon |
| 134 | +
|
| 135 | + >>> sequences = ["list", "tuple", "set", "tuple"] |
| 136 | + >>> len(sequences) == len(set(sequences)) |
| 137 | + False |
88 | 138 |
|
89 | 139 | Checks |
90 | 140 | ------ |
91 | 141 |
|
92 | | -* How many elements does a set have if it is formed from the following list |
93 | | - ``[4, 2, 3, 2, 1]``? |
| 142 | +* How many elements does a set have if it is formed from the following list ``[4, |
| 143 | + 2, 3, 2, 1]``? |
0 commit comments