• No results found

Operations with Sets

In document Programming with Python [2017].pdf (Page 123-131)

Additional Operations with Sequences

6.5 Operations with Sets

Set in Python is essentially the implementation of the set concept in mathematics.

A set is a collection of well-defined, unique but unordered entities/objects. The operations/methods for sets essentially correspond to the basic set operations in mathematics.

The Python Interpreter sequence in Fig.6.12a is intended to bring out the fea-tures of different operations with sets. A set can be formed from any collection of individual (diverse) objects. ff in [1] is the set formed with the elements in the listof integers—[2, 3, 5, 2, 7, 1]—as its members. ff in [2] has all the integers in the list without duplication. The integer‘2’ occurs twice in the list but only once in the setff. The order of the members in the set is immaterial; hence the numbers in it—that is the members of the set—cannot be indexed; but they can be Table 6.1 Execution of pop-append sequence in succession in Python Interpreter sequence in Fig.6.11

Jk aa an Elements and

indices in aa after

>>> ff = set([2, 3, 5, 2, 7, 1]) [1]

>>> as1, as2, as3 = set(aa1), set(aa2), set(aa3) [7][8]

>>> as1, as2, as3 [9]

({'h7', 'a0', 'l11', 'k10', 'g6', 'e4', 'b1', 'd3', 'f5', 'c2', 'i8', 'j9'}, {'h7', 'l11', 'k10', 'g6', 'e4', 'f5', 'i8', 'j9'}, {'h7', 'a0', 'g6', 'e4', 'b1', 'd3', 'f5', 'c2'})

>>> aal1, aas1 = len(aa1), len(as1) [10]

>>> aal1, aas1 [11]

>>> as4 = as3-as1 [19]

>>> as4

>>> as44 = as3.difference(as1) [23]

>>> as44 [24]

{'1B'}

(a)

Fig. 6.12 a Python Interpreter sequence illustrating use of some methods and operations with sets (continued) b Python Interpreter sequence illustrating use of some methods and operations with sets (continued) c Python Interpreter sequence illustrating use of some methods and operations with sets (continued) d Python Interpreter sequence illustrating use of some methods and operations with sets (continued)

116 6 Additional Operations with Sequences

>>> as44 == as4 [25]

True>>> as66, as6 = as1.intersection(as3), as1 & as3 [26]

>>> as6, as66 [27]

({'h7', 'a0', 'g6', 'e4', 'b1', 'd3', 'f5', 'c2'}, {'h7', 'a0', 'g6', 'e4', 'b1', 'd3', 'f5', 'c2'})

>>> as77, as7 = as1.union(as3), as1|as3 [28]

>>> as7, as77 [29]

({'h7', 'a0', 'l11', 'k10', 'g6', 'e4', '1B', 'b1', 'd3', 'f5', 'c2', 'i8', 'j9'}, {'h7', 'a0', 'l11', 'k10', 'g6', 'e4', '1B', 'b1', 'd3', 'f5', 'c2', 'i8', 'j9'})

>>> as8, as88 = as1&as2&as3,

as1.intersection(as2).intersection(as3) [30]

>>> as8, as88 [31]

({'h7', 'g6', 'e4', 'f5'}, {'h7', 'g6', 'e4', 'f5'})

>>> as9, as99 = as1^as3, as1.symmetric_difference(as3)[32]

>>> as9, as99 [33]

({'l11', 'k10', '1B', 'i8', 'j9'}, {'l11', 'k10', '1B', 'i8', 'j9'})

>>> st1 = 'interjection' [34]

>>> st2 = 'interruption' [35]

>>> st1s, st2s = set(st1), set(st2) [36]

>>> st1s, st2s [37]

({'j', 'i', 'o', 'e', 'r', 't', 'n', 'c'}, {'p', 'i', 'o', 'e', 'r', 't', 'n','u'})

>>> au1 = as1.union(aa2) [38]

>>> au1 [39]

{'h7', 'a0', 'l11', 'k10', 'g6', 'e4', 'b1', 'd3', 'f5', 'c2', 'i8', 'j9'}

>>> su1 = st1s.union(st2) [40]

>>> su1 [41]

{'p', 'j', 'i', 'o', 'e', 'r', 't', 'n', 'u', 'c'}

>>>

su2,su3,su4=st1s.intersection(st2),st1s.difference(st2),st

1s.symmetric_difference(st2) [42]

>>> su2, su3, su4 [43]

({'i', 'o', 'e', 'r', 't', 'n'}, {'j', 'c'}, {'p', 'j',

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

KeyError: 'x' [47]

counted.aa1, aa2, and aa3 are lists of strings separately defined in [3], [4], and [7].aa1.extend(aa2) in [5] uses the method ‘extend()’ to add all the elements ofaa2 to the list aa1. The enhanced aa1 in [6] has ‘e4’, ‘f5’, ‘g6’, and ‘h7’

fromaa2 added to aa1 in the same order—though they are already present in it. In fact the two‘e4’s in the enhanced aa1 are separate entities—each with its own

>>> su2.pop() [50]

>>> su2c = su2.copy() [55]

>>> su2c [56]

{'t', 'n', 'o', 'e'}

>>> su2 == su2c [57]

True

>>> su2 is su2c [58]

False

118 6 Additional Operations with Sequences

separate index—4 and 8 respectively; same holds good of ‘f5’, ‘g6’, and ‘h7’ also.

as1, as2, and as3 in [8] and [9] are the sets formed from the lists aa1, aa2, and aa3 respectively. as1 does not have any duplicate entries—the list aa1 has 16 elements in it whereas—the set formed from it (as1) has only 12 (distinct) elements in it—as can be seen from [10] and [11]. as3.remove(‘b1’) in [13] removes the element‘b1’ from the set as3—as can be seen by comparing as3 in [14] with that in [12]. ‘b1’ has been added to as3—through as3.add(‘b1’) in [15]; the restored version is in [16]. An attempt to add‘b1’ again in [17] does not alter as3—‘b1’

>>> as1 [75]

{'d3', 'a0', 'h7', 'f5', 'e4', 'b1', 'j9', 'c2', 'g6'}

>>> as1 &= as3 [76]

>>> as1 [77]

>>> as4, ast1 = set('interesting'), 'interposing'

>>> as4

being already present in it. Butas3.add(‘1B’) in [18] adds ‘1B’ as an additional element toas3—as can be seen from the following lines.

setsas1 and as3 as well as sets formed through the various possible opera-tions are shown in the respective Venn diagrams in Fig.6.13 (Sullivan 2008).

as4 = as3 − as1 as in [19] forms the set as4 from as3 and as1; as can be seen from the corresponding Venn diagram in Fig.6.13all the elements inas3 which are also present inas1 are removed. The remaining elements of as3 form as4; but as3 and as1 remain unaltered. With this as4 is the set of a single element ‘1B’

[20]. Similarlyas1–as3 in [21] forms as5 as the set of elements of as1 left behind by removing elements common with as3 from it. as1.difference (as3) achieves the same using the method difference() as in [23] and [24]. as4 (=as3 − as1) in [19] and as44 (=as3.difference(as1)) in [23] produce identical sets. The Python Interpreter sequence continues in Fig.6.12b. [25] con-firms the equality of as4 and as44. But as4 and as44 remain distinct objects.

as1 & as3 and as1.intersection (as3) use the ‘&’ operator and its method counterpart; both form new sets with elements common toas1 and as3—

assigned toas6 and as66 in [26]. Both are identical but distinct sets [27]. Their formation can be understood from the corresponding Venn diagram in Fig.6.13.

as1| as3 in [28] forms the union of as1 and as3 and assigns it to as7.

as77 = as1.union(as3) is its method based counterpart [28]. The union and intersection operations can be carried out with multiple sets as arguments as well.

as1

as3 as5=as1- as3 as9=as1^ as3

as4 =as3- as1

as6=as1&as3 as7=as1| as3

Fig. 6.13 Venn diagrams for different operations with as1 and as3

120 6 Additional Operations with Sequences

[30] shows an example with the three setsas1, as2, and as3. as8 and as88 in [30] and [31] combine all the elements common to all the three of them.

With the‘^’ operator, as1 ^ as3 in [32] forms the set as9 from as1 and as3;

as9 in [33] has all the elements of as1 and as3 which are not common to as1 and as3. For example ‘a0’ is present in as1 as well as as3; it is left out of as9. ‘1B’ is present only inas3; it is included in as9. Similarly ‘k10’ present in as3 but not in as1; it is included in as9. as1.symmetric_difference(as3) is the method corresponding to the‘^’ operation (yielding as 99).

st1 (‘interjection’) in [34] and st2(‘interruption’) in [35] respectively are strings (see Fig.6.12b).st1s and st2 s in [35] and [37] are the sets of integers formed out of the elements (characters) forming the stringsst1 and st2 respectively—that—is the set of letters present in ‘interjection’ and is the set of letters present in

‘interruption’.

With pp as a set and qq—a list, a string or any other compatible sequence−rr = pp.union(qq) generates the set rr as the union of pp and the elements ofqq. Such mixed mode of forming/generating a set is possible only with the method (.union()) but not with the operator (|). Union of the elements of the setas1 and the list aa2 is assigned to set au1 in [38]. au1 so formed is in [39], [40] and [41] show the formation of a setsu1 as a union of the set st1s and the string st2. Similarly intersection, difference, and symmet-ric_differencemethods use a set as a base argument; the second argument (as well as other arguments if present) can be sequences of other types. Examples are in [42]. Setsu2 is the set of intersection of set st1s and string st2. su3 is the set of difference of setst1s and string st2; su4 is the set of symmet-ric_differenceof setst1s and string st2. [43] gives respective details.

pp.remove(qq) removes the element qq from the set pp. if qq is not present in pp the Python sequence rises a ‘KeyErrror’. Thus su2.remove(‘r’) in [44]

removes‘r’ as can be seen by comparing su2 in [43] and [45]. su2.remove(‘x’) in [46] returns a‘KeyError’ [47] since su2 does not have ‘x’ in it as a member. The method discard() is similar to the method remove(), to a certain extent. su2.

discard(‘x’) discards (removes) ‘x’ from su2, if it is present. If ‘x’ is not present insu2, no action follows as can be seen from [48] & [49].

The Python interpreter sequence continues in Fig.6.12c. pp.pop() pops an element at random frompp. su2.pop() in [50] pops ‘i’ from su2 leaving it with

‘o’, ‘e’, ‘t’, and ‘n’ as its contents. pp.clear() clears the set pp. As an example su2.clear() in [52] clears su2 and leaves it empty [53]. rr = pp.copy() creates rr as a new set—a replica of pp itself. su2c is a copy of su2—[55, 56]. Being copies they are identical as can be seen from [57]; but they are distinct objects [58].

For a given setas3 [59] list(as3) forms a list with all the elements of as3 being in it. The function while as3:alt.append(as3.pop()) in [60]

achieves the same though in a roundabout manner.alt has been defined as an empty list beforehand [59]. ‘while as3:alt.append(as3.pop())’ pops elements at random fromas3 and appends the popped element to list alt; this continues until as3 is empty [62].

pp.intersection(qq) has pp as a set and qq a compatible sequence and generates rr as the corresponding enhanced set. pp.intersection_update (qq) is a variant where the set pp itself is updated, its elements being only those common withqq. pp & = qq achieves the same with the restriction that qq too has to be a set. [69], [70] and [76], [77] in the Python Interpreter sequence are illustrations. Similar update variants are available with ‘|’ (‘|=’), ‘^’(‘^=’), and

‘−‘(‘−=’) as well. In all these cases the operator versions (‘|=’, ‘^=’, ‘−=’) can be used only if all the arguments on either side are sets. The respective methods are more flexible and can have the second argument as any mutable sequence. The illustrative examples for these follow from [76] onwards in the Python Interpreter sequence continued in Fig.6.12d.

6.6 Frozensets

Any set is mutable; methods like remove(), extend(), pop() can be used with sets to add or remove elements from it. In contrast a frozenset is a‘rigid’ set.

Once formed it remains frozen (as with a tuple). Additional elements cannot be added to it; nor can elements be removed from it. The Python Intrpreter sequence in Fig.6.14 illustrates its formation. aa1 in [1] is a list of strings. ‘e4’, ‘f5’,

‘g6’, and ‘h7’ as its elements, occur twice in it. frozenset(aa1) (=afz1) forms a frozenset of its members and assigns it toafz1. afz1 in [3] has every element as a unique one without duplication; order is not maintained either. [4] forms a set as1 out of aa1. Content wise as1 is identical to afz1 as can be seen from [6].

The methods and operations with sets are applicable to frozensets also; the only exceptions are the update-type methods and their operator counterparts. They are not applicable to frozensets since they change the set by adding elements to it or removing elements from it. The usage being similar to that with sets, the methods/operations are not separately illustrated with frozensets.

>>> aa1 = ['a0', 'b1', 'c2', 'd3', 'e4', 'f5', 'g6', 'h7', 'e4', 'f5', 'g6', 'h7', 'i8', 'j9', 'k10', 'l11'] [1]

>>> afz1 = frozenset(aa1) [2]

>>> afz1 [3]

frozenset({'f5', 'c2', 'j9', 'd3', 'h7', 'a0', 'i8', 'l11', 'g6', 'e4', 'b1', 'k10'})

>>> as1 = set(aa1) [4]

>>> as1 [5]

{'f5', 'c2', 'j9', 'd3', 'h7', 'a0', 'i8', 'l11', 'g6', 'e4', 'b1', 'k10'}

>>> as1 == afz1 [6]

True

>>>

Fig. 6.14 Python Interpreter sequence illustrating formation of frozenset

122 6 Additional Operations with Sequences

In document Programming with Python [2017].pdf (Page 123-131)