forked from duliodenis/mit-6.0001-intro-cs-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec5_tuples_lists.py
219 lines (184 loc) · 4.46 KB
/
lec5_tuples_lists.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
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
#########################
## EXAMPLE: returning a tuple
#########################
def quotient_and_remainder(x, y):
q = x // y
r = x % y
return (q, r)
(quot, rem) = quotient_and_remainder(5,3)
print(quot)
print(rem)
#########################
## EXAMPLE: iterating over tuples
#########################
def get_data(aTuple):
"""
aTuple, tuple of tuples (int, string)
Extracts all integers from aTuple and sets
them as elements in a new tuple.
Extracts all unique strings from from aTuple
and sets them as elements in a new tuple.
Returns a tuple of the minimum integer, the
maximum integer, and the number of unique strings
"""
nums = () # empty tuple
words = ()
for t in aTuple:
# concatenating with a singleton tuple
nums = nums + (t[0],)
# only add words haven't added before
if t[1] not in words:
words = words + (t[1],)
min_n = min(nums)
max_n = max(nums)
unique_words = len(words)
return (min_n, max_n, unique_words)
test = ((1,"a"),(2, "b"),
(1,"a"),(7,"b"))
(a, b, c) = get_data(test)
print("a:",a,"b:",b,"c:",c)
# apply to any data you want!
tswift = ((2014,"Katy"),
(2014, "Harry"),
(2012,"Jake"),
(2010,"Taylor"),
(2008,"Joe"))
(min_year, max_year, num_people) = get_data(tswift)
print("From", min_year, "to", max_year, \
"Taylor Swift wrote songs about", num_people, "people!")
#########################
## EXAMPLE: sum of elements in a list
#########################
def sum_elem_method1(L):
total = 0
for i in range(len(L)):
total += L[i]
return total
def sum_elem_method2(L):
total = 0
for i in L:
total += i
return total
print(sum_elem_method1([1,2,3,4]))
print(sum_elem_method2([1,2,3,4]))
#########################
## EXAMPLE: various list operations
## put print(L) at different locations to see how it gets mutated
#########################
L1 = [2,1,3]
L2 = [4,5,6]
L3 = L1 + L2
L1.extend([0,6])
L = [2,1,3,6,3,7,0]
L.remove(2)
L.remove(3)
del(L[1])
print(L.pop())
s = "I<3 cs"
print(list(s))
print(s.split('<'))
L = ['a', 'b', 'c']
print(''.join(L))
print('_'.join(L))
L=[9,6,0,3]
print(sorted(L))
L.sort()
L.reverse()
#########################
## EXAMPLE: aliasing
#########################
a = 1
b = a
print(a)
print(b)
warm = ['red', 'yellow', 'orange']
hot = warm
hot.append('pink')
print(hot)
print(warm)
#########################
## EXAMPLE: cloning
#########################
cool = ['blue', 'green', 'grey']
chill = cool[:]
chill.append('black')
print(chill)
print(cool)
#########################
## EXAMPLE: sorting with/without mutation
#########################
warm = ['red', 'yellow', 'orange']
sortedwarm = warm.sort()
print(warm)
print(sortedwarm)
cool = ['grey', 'green', 'blue']
sortedcool = sorted(cool)
print(cool)
print(sortedcool)
#########################
## EXAMPLE: lists of lists of lists...
#########################
warm = ['yellow', 'orange']
hot = ['red']
brightcolors = [warm]
brightcolors.append(hot)
print(brightcolors)
hot.append('pink')
print(hot)
print(brightcolors)
###############################
## EXAMPLE: mutating a list while iterating over it
###############################
def remove_dups(L1, L2):
for e in L1:
if e in L2:
L1.remove(e)
def remove_dups_new(L1, L2):
L1_copy = L1[:]
for e in L1_copy:
if e in L2:
L1.remove(e)
L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6]
remove_dups(L1, L2)
print(L1, L2)
L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6]
remove_dups_new(L1, L2)
print(L1, L2)
###############################
## EXERCISE: Test yourself by predicting what the output is and
## what gets mutated then check with the Python Tutor
###############################
cool = ['blue', 'green']
warm = ['red', 'yellow', 'orange']
print(cool)
print(warm)
colors1 = [cool]
print(colors1)
colors1.append(warm)
print('colors1 = ', colors1)
colors2 = [['blue', 'green'],
['red', 'yellow', 'orange']]
print('colors2 =', colors2)
warm.remove('red')
print('colors1 = ', colors1)
print('colors2 =', colors2)
for e in colors1:
print('e =', e)
for e in colors1:
if type(e) == list:
for e1 in e:
print(e1)
else:
print(e)
flat = cool + warm
print('flat =', flat)
print(flat.sort())
print('flat =', flat)
new_flat = sorted(flat, reverse = True)
print('flat =', flat)
print('new_flat =', new_flat)
cool[1] = 'black'
print(cool)
print(colors1)