generated from r4ds/bookclub-template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
03_notes.qmd
1115 lines (663 loc) · 19.3 KB
/
03_notes.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Data Structures and Sequences
## Tuples
![](https://pynative.com/wp-content/uploads/2021/02/python-tuple.jpg)
A tuple is a fixed-length, immutable sequence of Python objects which, once assigned, cannot be changed. The easiest way to create one is with a comma-separated sequence of values wrapped in parentheses:
```{python}
tup = (4, 5, 6)
tup
```
In many contexts, the parentheses can be omitted
```{python}
tup = 4, 5, 6
tup
```
You can convert any sequence or iterator to a tuple by invoking
```{python}
tuple([4,0,2])
tup = tuple('string')
tup
```
Elements can be accessed with square brackets []
Note the zero indexing
```{python}
tup[0]
```
Tuples of tuples
```{python}
nested_tup = (4,5,6),(7,8)
nested_tup
```
```{python}
nested_tup[0]
```
```{python}
nested_tup[1]
```
While the objects stored in a tuple may be mutable themselves, once the tuple is created it’s not possible to modify which object is stored in each slot:
```{python}
tup = tuple(['foo', [1, 2], True])
tup[2]
```
```{{python}}
tup[2] = False
```
````
TypeError Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 tup[2] = False
TypeError: 'tuple' object does not support item assignment
TypeError: 'tuple' object does not support item assignment
````
If an object inside a tuple is mutable, such as a list, you can modify it in place
```{python}
tup[1].append(3)
tup
```
You can concatenate tuples using the + operator to produce longer tuples:
```{python}
(4, None, 'foo') + (6, 0) + ('bar',)
```
### Unpacking tuples
If you try to assign to a tuple-like expression of variables, Python will attempt to unpack the value on the righthand side of the equals sign:
```{python}
tup = (4, 5, 6)
tup
```
```{python}
a, b, c = tup
c
```
Even sequences with nested tuples can be unpacked:
```{python}
tup = 4, 5, (6,7)
a, b, (c, d) = tup
d
```
To easily swap variable names
```{python}
a, b = 1, 4
a
```
```{python}
b
```
```{python}
b, a = a, b
a
```
```{python}
b
```
A common use of variable unpacking is iterating over sequences of tuples or lists
```{python}
seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
seq
```
```{python}
for a, b, c in seq:
print(f'a={a}, b={b}, c={c}')
```
`*rest` syntax for plucking elements
```{python}
values = 1,2,3,4,5
a, b, *rest = values
rest
```
As a matter of convention, many Python programmers will use the underscore (_) for unwanted variables:
```{python}
a, b, *_ = values
```
### Tuple methods
Since the size and contents of a tuple cannot be modified, it is very light on instance methods. A particularly useful one (also available on lists) is `count`
```{python}
a = (1,2,2,2,2,3,4,5,7,8,9)
a.count(2)
```
## List
![](https://pynative.com/wp-content/uploads/2021/03/python-list.jpg)
In contrast with tuples, lists are variable length and their contents can be modified in place.
Lists are mutable.
Lists use `[]` square brackts or the `list` function
```{python}
a_list = [2, 3, 7, None]
tup = ("foo", "bar", "baz")
b_list = list(tup)
b_list
```
```{python}
b_list[1] = "peekaboo"
b_list
```
Lists and tuples are semantically similar (though tuples cannot be modified) and can be used interchangeably in many functions.
```{python}
gen = range(10)
gen
```
```{python}
list(gen)
```
### Adding and removing list elements
the `append` method
```{python}
b_list.append("dwarf")
b_list
```
the `insert` method
```{python}
b_list.insert(1, "red")
b_list
```
`insert` is computationally more expensive than `append`
the `pop` method, the inverse of `insert`
```{python}
b_list.pop(2)
```
```{python}
b_list
```
the `remove` method
```{python}
b_list.append("foo")
b_list
```
```{python}
b_list.remove("foo")
b_list
```
Check if a list contains a value using the `in` keyword:
```{python}
"dwarf" in b_list
```
The keyword `not` can be used to negate an `in`
```{python}
"dwarf" not in b_list
```
### Concatenating and combining lists
similar with tuples, use `+` to concatenate
```{python}
[4, None, "foo"] + [7, 8, (2, 3)]
```
the `extend` method
```{python}
x = [4, None, "foo"]
x.extend([7,8,(2,3)])
x
```
list concatenation by addition is an expensive operation
using `extend` is preferable
```{{python}}
everything = []
for chunk in list_of_lists:
everything.extend(chunk)
```
is generally faster than
```{{python}}
everything = []
for chunk in list_of_lists:
everything = everything + chunk
```
### Sorting
the `sort` method
```{python}
a = [7, 2, 5, 1, 3]
a.sort()
a
```
`sort` options
```{python}
b = ["saw", "small", "He", "foxes", "six"]
b.sort(key = len)
b
```
### Slicing
Slicing semantics takes a bit of getting used to, especially if you’re coming from R or MATLAB.
using the indexing operator `[]`
```{python}
seq = [7, 2, 3, 7, 5, 6, 0, 1]
seq[3:5]
```
also assigned with a sequence
```{python}
seq[3:5] = [6,3]
seq
```
Either the `start` or `stop` can be omitted
```{python}
seq[:5]
```
```{python}
seq[3:]
```
Negative indices slice the sequence relative to the end:
```{python}
seq[-4:]
```
A step can also be used after a second colon to, say, take every other element:
```{python}
seq[::2]
```
A clever use of this is to pass -1, which has the useful effect of reversing a list or tuple:
```{python}
seq[::-1]
```
## Dictionary
![](https://pynative.com/wp-content/uploads/2021/02/dictionaries-in-python.jpg)
The dictionary or dict may be the most important built-in Python data structure.
One approach for creating a dictionary is to use curly braces {} and colons to separate keys and values:
```{python}
empty_dict = {}
d1 = {"a": "some value", "b": [1, 2, 3, 4]}
d1
```
access, insert, or set elements
```{python}
d1[7] = "an integer"
d1
```
and as before
```{python}
"b" in d1
```
the `del` and `pop` methods
```{python}
del d1[7]
d1
```
```{python}
ret = d1.pop("a")
ret
```
The `keys` and `values` methods
```{python}
list(d1.keys())
```
```{python}
list(d1.values())
```
the `items` method
```{python}
list(d1.items())
```
the update method to merge one dictionary into another
```{python}
d1.update({"b": "foo", "c": 12})
d1
```
### Creating dictionaries from sequences
```{python}
list(range(5))
```
```{python}
tuples = zip(range(5), reversed(range(5)))
tuples
mapping = dict(tuples)
mapping
```
### Default values
imagine categorizing a list of words by their first letters as a dictionary of lists
```{python}
words = ["apple", "bat", "bar", "atom", "book"]
by_letter = {}
for word in words:
letter = word[0]
if letter not in by_letter:
by_letter[letter] = [word]
else:
by_letter[letter].append(word)
by_letter
```
The `setdefault` dictionary method can be used to simplify this workflow. The preceding for loop can be rewritten as:
```{python}
by_letter = {}
for word in words:
letter = word[0]
by_letter.setdefault(letter, []).append(word)
by_letter
```
The built-in `collections`module has a useful class, `defaultdict`, which makes this even easier.
```{python}
from collections import defaultdict
by_letter = defaultdict(list)
for word in words:
by_letter[word[0]].append(word)
by_letter
```
### Valid dictionary key types
keys generally have to be immutable objects like scalars or tuples for *hashability*
To use a list as a key, one option is to convert it to a tuple, which can be hashed as long as its elements also can be:
```{python}
d = {}
d[tuple([1,2,3])] = 5
d
```
## Set
![](https://pynative.com/wp-content/uploads/2021/03/python-sets.jpg)
can be created in two ways: via the `set` function or via a `set literal` with curly braces:
```{python}
set([2, 2, 2, 1, 3, 3])
{2,2,1,3,3}
```
Sets support mathematical set operations like union, intersection, difference, and symmetric difference.
The `union` of these two sets:
```{python}
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
a.union(b)
a | b
```
The `&`operator or the `intersection` method
```{python}
a.intersection(b)
a & b
```
[A table of commonly used `set` methods](https://wesmckinney.com/book/python-builtin.html#tbl-table_set_operations)
All of the logical set operations have in-place counterparts, which enable you to replace the contents of the set on the left side of the operation with the result. For very large sets, this may be more efficient
```{python}
c = a.copy()
c |= b
c
```
```{python}
d = a.copy()
d &= b
d
```
set elements generally must be immutable, and they must be hashable
you can convert them to tuples
You can also check if a set is a subset of (is contained in) or a superset of (contains all elements of) another set
```{python}
a_set = {1, 2, 3, 4, 5}
{1, 2, 3}.issubset(a_set)
```
```{python}
a_set.issuperset({1, 2, 3})
```
## Built-In Sequence Functions
### enumerate
`enumerate` returns a sequence of (i, value) tuples
### sorted
`sorted` returns a new sorted list
```{python}
sorted([7,1,2,9,3,6,5,0,22])
```
### zip
`zip` “pairs” up the elements of a number of lists, tuples, or other sequences to create a list of tuples
```{python}
seq1 = ["foo", "bar", "baz"]
seq2 = ["one", "two", "three"]
zipped = zip(seq1, seq2)
list(zipped)
```
`zip` can take an arbitrary number of sequences, and the number of elements it produces is determined by the shortest sequence
```{python}
seq3 = [False, True]
list(zip(seq1, seq2, seq3))
```
A common use of `zip` is simultaneously iterating over multiple sequences, possibly also combined with `enumerate`
```{python}
for index, (a, b) in enumerate(zip(seq1, seq2)):
print(f"{index}: {a}, {b}")
```
`reversed` iterates over the elements of a sequence in reverse order
```{python}
list(reversed(range(10)))
```
## List, Set, and Dictionary Comprehensions
```
[expr for value in collection if condition]
```
For example, given a list of strings, we could filter out strings with length 2 or less and convert them to uppercase like this
```{python}
strings = ["a", "as", "bat", "car", "dove", "python"]
[x.upper() for x in strings if len(x) > 2]
```
A dictionary comprehension looks like this
```
dict_comp = {key-expr: value-expr for value in collection
if condition}
```
Suppose we wanted a set containing just the lengths of the strings contained in the collection
```{python}
unique_lengths = {len(x) for x in strings}
unique_lengths
```
we could create a lookup map of these strings for their locations in the list
```{python}
loc_mapping = {value: index for index, value in enumerate(strings)}
loc_mapping
```
## Nested list comprehensions
Suppose we have a list of lists containing some English and Spanish names. We want to get a single list containing all names with two or more a’s in them
```{python}
all_data = [["John", "Emily", "Michael", "Mary", "Steven"],
["Maria", "Juan", "Javier", "Natalia", "Pilar"]]
result = [name for names in all_data for name in names
if name.count("a") >= 2]
result
```
Here is another example where we “flatten” a list of tuples of integers into a simple list of integers
```{python}
some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
flattened = [x for tup in some_tuples for x in tup]
flattened
```
# Functions
![](https://miro.medium.com/max/1200/1*ZegxhR33NdeVRpBPYXnYYQ.gif)
`Functions` are the primary and most important method of code organization and reuse in Python.
they use the `def` keyword
Each function can have positional arguments and keyword arguments. Keyword arguments are most commonly used to specify default values or optional arguments. Here we will define a function with an optional z argument with the default value 1.5
```{python}
def my_function(x, y, z=1.5):
return (x + y) * z
my_function(4,25)
```
The main restriction on function arguments is that the keyword arguments must follow the positional arguments
## Namespaces, Scope, and Local Functions
A more descriptive name describing a variable scope in Python is a namespace.
Consider the following function
```{python}
a = []
def func():
for i in range(5):
a.append(i)
```
When `func()` is called, the empty list a is created, five elements are appended, and then a is destroyed when the function exits.
```{python}
func()
func()
a
```
## Returing Multiple Values
What’s happening here is that the function is actually just returning one object, a tuple, which is then being unpacked into the result variables.
```{python}
def f():
a = 5
b = 6
c = 7
return a, b, c
a, b, c = f()
a
```
## Functions are Objects
Suppose we were doing some data cleaning and needed to apply a bunch of transformations to the following list of strings:
```{python}
states = [" Alabama ", "Georgia!", "Georgia", "georgia", "FlOrIda",
"south carolina##", "West virginia?"]
import re
def clean_strings(strings):
result = []
for value in strings:
value = value.strip()
value = re.sub("[!#?]", "", value)
value = value.title()
result.append(value)
return result
clean_strings(states)
```
Another approach
```{python}
def remove_punctuation(value):
return re.sub("[!#?]", "", value)
clean_ops = [str.strip, remove_punctuation, str.title]
def clean_strings(strings, ops):
result = []
for value in strings:
for func in ops:
value = func(value)
result.append(value)
return result
clean_strings(states, clean_ops)
```
You can use functions as arguments to other functions like the built-in `map` function
```{python}
for x in map(remove_punctuation, states):
print(x)
```
## Anonymous Lambda Functions
a way of writing functions consisting of a single statement
suppose you wanted to sort a collection of strings by the number of distinct letters in each string
```{python}
strings = ["foo", "card", "bar", "aaaaaaa", "ababdo"]
strings.sort(key=lambda x: len(set(x)))
strings
```
# Generators
Many objects in Python support iteration, such as over objects in a list or lines in a file.
```{python}
some_dict = {"a": 1, "b": 2, "c": 3}
for key in some_dict:
print(key)
```
Most methods expecting a list or list-like object will also accept any iterable object. This includes built-in methods such as `min`, `max`, and `sum`, and type constructors like `list` and `tuple`
A `generator` is a convenient way, similar to writing a normal function, to construct a new iterable object. Whereas normal functions execute and return a single result at a time, generators can return a sequence of multiple values by pausing and resuming execution each time the generator is used. To create a generator, use the yield keyword instead of return in a function
```{python}
def squares(n=10):
print(f"Generating squares from 1 to {n ** 2}")
for i in range(1, n + 1):
yield i ** 2
gen = squares()
for x in gen:
print(x, end=" ")
```
> Since generators produce output one element at a time versus an entire list all at once, it can help your program use less memory.
## Generator expressions
This is a generator analogue to list, dictionary, and set comprehensions. To create one, enclose what would otherwise be a list comprehension within parentheses instead of brackets:
```{python}
gen = (x ** 2 for x in range(100))
gen
```
Generator expressions can be used instead of list comprehensions as function arguments in some cases:
```{python}
sum(x ** 2 for x in range(100))
```
```{python}
dict((i, i ** 2) for i in range(5))
```
## itertools module
`itertools` module has a collection of generators for many common data algorithms.
`groupby` takes any sequence and a function, grouping consecutive elements in the sequence by return value of the function
```{python}
import itertools
def first_letter(x):
return x[0]
names = ["Alan", "Adam", "Jackie", "Lily", "Katie", "Molly"]
for letter, names in itertools.groupby(names, first_letter):
print(letter, list(names))
```
[Table of other itertools functions](https://wesmckinney.com/book/python-builtin.html#tbl-table_itertools)
# Errors and Exception Handling
Handling errors or exceptions gracefully is an important part of building robust programs
```{python}
def attempt_float(x):
try:
return float(x)
except:
return x
attempt_float("1.2345")
```
```{python}
attempt_float("something")
```
You might want to suppress only ValueError, since a TypeError (the input was not a string or numeric value) might indicate a legitimate bug in your program. To do that, write the exception type after except:
```{python}
def attempt_float(x):
try:
return float(x)
except ValueError:
return x
```
```{python}