Mastering Python Second Edition Release Code

This commit is contained in:
Rick van Hattem
2022-05-05 18:25:55 +02:00
commit 3223a43fe3
454 changed files with 20230 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
>>> import collections
>>> counter = collections.Counter('eggs')
>>> for character in 'eggs':
... print(f'Count for {character}: {counter[character]}')
Count for e: 1
Count for g: 2
Count for g: 2
Count for s: 1
------------------------------------------------------------------------------
>>> import math
>>> import collections
>>> counter = collections.Counter()
>>> for i in range(0, 100000):
... counter[math.sqrt(i) // 25] += 1
>>> for key, count in counter.most_common(5):
... print(f'{key}: {count}')
11.0: 14375
10.0: 13125
9.0: 11875
8.0: 10625
12.0: 10000
------------------------------------------------------------------------------
>>> import collections
>>> def print_sorted(counter):
... sorted_characters = sorted(counter.elements())
... print(''.join(sorted_characters))
>>> eggs = collections.Counter('eggs')
>>> spam = collections.Counter('spam')
>>> print_sorted(eggs)
eggs
>>> print_sorted(spam)
amps
>>> print_sorted(eggs & spam)
s
>>> print_sorted(spam & eggs)
s
>>> print_sorted(eggs - spam)
egg
>>> print_sorted(spam - eggs)
amp
>>> print_sorted(eggs + spam)
aeggmpss
>>> print_sorted(spam + eggs)
aeggmpss
>>> print_sorted(eggs | spam)
aeggmps
>>> print_sorted(spam | eggs)
aeggmps
+49
View File
@@ -0,0 +1,49 @@
>>> import collections
>>> queue = collections.deque()
>>> queue.append(1)
>>> queue.append(2)
>>> queue
deque([1, 2])
>>> queue.popleft()
1
>>> queue.popleft()
2
>>> queue.popleft()
Traceback (most recent call last):
...
IndexError: pop from an empty deque
------------------------------------------------------------------------------
>>> import collections
>>> queue = collections.deque()
>>> queue.append(1)
>>> queue.append(2)
>>> queue
deque([1, 2])
>>> queue.pop()
2
>>> queue.pop()
1
>>> queue.pop()
Traceback (most recent call last):
...
IndexError: pop from an empty deque
------------------------------------------------------------------------------
>>> import collections
>>> circular = collections.deque(maxlen=2)
>>> for i in range(5):
... circular.append(i)
... circular
deque([0], maxlen=2)
deque([0, 1], maxlen=2)
deque([1, 2], maxlen=2)
deque([2, 3], maxlen=2)
deque([3, 4], maxlen=2)
>>> circular
deque([3, 4], maxlen=2)
+22
View File
@@ -0,0 +1,22 @@
>>> import itertools
>>> list(itertools.combinations(range(3), 2))
[(0, 1), (0, 2), (1, 2)]
------------------------------------------------------------------------------
>>> import itertools
>>> list(itertools.combinations_with_replacement(range(3), 2))
[(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]
------------------------------------------------------------------------------
>>> import itertools
>>> def powerset(iterable):
... return itertools.chain.from_iterable(
... itertools.combinations(iterable, i)
... for i in range(len(iterable) + 1))
>>> list(powerset(range(3)))
[(), (0,), (1,), (2,), (0, 1), (0, 2), (1, 2), (0, 1, 2)]
+22
View File
@@ -0,0 +1,22 @@
>>> import collections
>>> Point = collections.namedtuple('Point', ['x', 'y', 'z'])
>>> point_a = Point(1, 2, 3)
>>> point_a
Point(x=1, y=2, z=3)
>>> point_b = Point(x=4, z=5, y=6)
>>> point_b
Point(x=4, y=6, z=5)
------------------------------------------------------------------------------
>>> x, y, z = point_a
>>> print(f'X: {x}, Y: {y}, Z: {z}')
X: 1, Y: 2, Z: 3
>>> print('X: {}, Y: {}, Z: {}'.format(*point_b))
X: 4, Y: 6, Z: 5
>>> print('X: %d, Y: %d, Z: %d' % point_b)
X: 4, Y: 6, Z: 5
>>> print(f'X: {point_a.x}')
X: 1
+4
View File
@@ -0,0 +1,4 @@
>>> import itertools
>>> list(itertools.permutations(range(3), 2))
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
+20
View File
@@ -0,0 +1,20 @@
>>> import collections
>>> spam = collections.OrderedDict()
>>> spam['b'] = 2
>>> spam['c'] = 3
>>> spam['a'] = 1
>>> spam
OrderedDict([('b', 2), ('c', 3), ('a', 1)])
>>> for key, value in spam.items():
... key, value
('b', 2)
('c', 3)
('a', 1)
>>> eggs = collections.OrderedDict(sorted(spam.items()))
>>> eggs
OrderedDict([('a', 1), ('b', 2), ('c', 3)])