-
Notifications
You must be signed in to change notification settings - Fork 19
/
PDA_note_week4.py
159 lines (114 loc) · 3.55 KB
/
PDA_note_week4.py
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
"""
Python Data Analysis
WEEK THREE
Course Note and Quiz
"""
SPLIT = "====================================="
print(SPLIT)
"""
Example code to sort sequences.
"""
import random
# Easily create a list of numbers
data = list(range(10))
print("range data:", data)
# Randomly shuffle those numbers
random.shuffle(data)
print("shuffled data:", data)
# Sort the list of numbers
data.sort()
print("sorted data:", data)
# Shuffle it again
random.shuffle(data)
print("shuffled data:", data)
# Use sorted to sort the list
newdata = sorted(data)
print("data after sorted:", data) # the sorted() does not change the original data
print("returned from sorted:", newdata)
# Convert to a tuple
datatup = tuple(data)
print("data tuple:", datatup)
# Sort the tuple of numbers
# datatup.sort(); does not work because tuple is unmutable
print("tuple after sort:", datatup)
# Use sorted to sort the tuple
newdatatup = sorted(datatup) # sorted will take anything that is iteratable
print("returned from sorted:", newdatatup)
# Create a dictionary of squares (dictionary comprehension)
datamap = {key: key ** 2 for key in datatup}
print("data dictionary:", datamap)
# Use sorted to sort the dictionary
sortmap = sorted(datamap) # will only sort the keys, sorted() will always return a list!
print("returned from sorted:", sortmap)
print(SPLIT)
"""
Examples of creating and using anonymous functions.
"""
# Easily create a list of numbers
data = list(range(10))
print("range data:", data)
def square(val):
return val ** 2
# Square all numbers in the list
squares = list(map(square, data)) # map(function, iterable, ...)
print("squares:", squares)
# Double all numbers in the list
doubles = list(map(lambda num: num * 2, data))
print("doubles:", doubles)
# Create a list of random numbers (list comprehension)
randnums = [random.randrange(2, num+3) for num in range(10)]
print("random numbers:", randnums)
# Create a list of tuples
tups = list(map(lambda num1, num2: (num1, num2), data, randnums))
print("tuples:", tups)
# Create a list of the min values in the tuples
mins = list(map(lambda pair: min(pair[0], pair[1]), tups))
print("minimums:", mins)
# Create a list only of tuples where the second item is less than the first
newtups = list(filter(lambda pair: pair[1] < pair[0], tups))
print("filtered:", newtups)
print(SPLIT)
"""
More advanced sorting examples.
"""
# Easily create a shuffled list of numbers
data = list(range(10))
random.shuffle(data)
print("shuffled data:", data)
# Sort the list of numbers
data.sort()
print("ascending sort:", data)
data.sort(reverse=True)
print("descending sort:", data)
# Create a list of tuples
datatups = [(item, random.randrange(3, 15)) for item in data]
print("data tuples:", datatups)
# Sort the list
datatups.sort()
print("sorted data tuples:", datatups)
datatups.sort(key=lambda pair: pair[1])
print("sorted by second item:", datatups)
datatups.sort(key=lambda pair: pair[0] * pair[1], reverse=True)
print("sorted by product:", datatups)
# Shuffle it again
random.shuffle(datatups)
print("shuffled tuples:", datatups)
# Use sorted to sort the list
newdata = sorted(datatups, key=lambda pair: pair[1], reverse=True)
print("tuples after sorted:", datatups)
print("returned from sorted:", newdata)
print(SPLIT)
"""
Tests for the final project
"""
my_dict = {'Tom': {'player': 'Tom', 'hits': 1, 'homers': 2},
'Jack': {'player': 'Jack', 'hits': 3, 'homers': 4}}
for key, value in my_dict.items():
print(key, value)
my_list_dict = []
for key in my_dict:
my_list_dict.append(my_dict[key])
for row in my_list_dict:
print(row)
print(my_list_dict)
print(SPLIT)