Attachment 'lec5_tuples_lists.py'
Download 1 #########################
2 ## EXAMPLE: returning a tuple
3 #########################
4 def quotient_and_remainder(x, y):
5 q = x // y
6 r = x % y
7 return (q, r)
8
9 (quot, rem) = quotient_and_remainder(5,3)
10 print(quot)
11 print(rem)
12
13
14 #########################
15 ## EXAMPLE: iterating over tuples
16 #########################
17 def get_data(aTuple):
18 """
19 aTuple, tuple of tuples (int, string)
20 Extracts all integers from aTuple and sets
21 them as elements in a new tuple.
22 Extracts all unique strings from from aTuple
23 and sets them as elements in a new tuple.
24 Returns a tuple of the minimum integer, the
25 maximum integer, and the number of unique strings
26 """
27 nums = () # empty tuple
28 words = ()
29 for t in aTuple:
30 # concatenating with a singleton tuple
31 nums = nums + (t[0],)
32 # only add words haven't added before
33 if t[1] not in words:
34 words = words + (t[1],)
35 min_n = min(nums)
36 max_n = max(nums)
37 unique_words = len(words)
38 return (min_n, max_n, unique_words)
39
40 test = ((1,"a"),(2, "b"),
41 (1,"a"),(7,"b"))
42 (a, b, c) = get_data(test)
43 print("a:",a,"b:",b,"c:",c)
44
45 # apply to any data you want!
46 tswift = ((2014,"Katy"),
47 (2014, "Harry"),
48 (2012,"Jake"),
49 (2010,"Taylor"),
50 (2008,"Joe"))
51 (min_year, max_year, num_people) = get_data(tswift)
52 print("From", min_year, "to", max_year, \
53 "Taylor Swift wrote songs about", num_people, "people!")
54
55 #########################
56 ## EXAMPLE: sum of elements in a list
57 #########################
58 def sum_elem_method1(L):
59 total = 0
60 for i in range(len(L)):
61 total += L[i]
62 return total
63
64 def sum_elem_method2(L):
65 total = 0
66 for i in L:
67 total += i
68 return total
69
70 print(sum_elem_method1([1,2,3,4]))
71 print(sum_elem_method2([1,2,3,4]))
72
73
74 #########################
75 ## EXAMPLE: various list operations
76 ## put print(L) at different locations to see how it gets mutated
77 #########################
78 L1 = [2,1,3]
79 L2 = [4,5,6]
80 L3 = L1 + L2
81 L1.extend([0,6])
82
83 L = [2,1,3,6,3,7,0]
84 L.remove(2)
85 L.remove(3)
86 del(L[1])
87 print(L.pop())
88
89 s = "I<3 cs"
90 print(list(s))
91 print(s.split('<'))
92 L = ['a', 'b', 'c']
93 print(''.join(L))
94 print('_'.join(L))
95
96 L=[9,6,0,3]
97 print(sorted(L))
98 L.sort()
99 L.reverse()
100
101
102 #########################
103 ## EXAMPLE: aliasing
104 #########################
105 a = 1
106 b = a
107 print(a)
108 print(b)
109
110 warm = ['red', 'yellow', 'orange']
111 hot = warm
112 hot.append('pink')
113 print(hot)
114 print(warm)
115
116 #########################
117 ## EXAMPLE: cloning
118 #########################
119 cool = ['blue', 'green', 'grey']
120 chill = cool[:]
121 chill.append('black')
122 print(chill)
123 print(cool)
124
125 #########################
126 ## EXAMPLE: sorting with/without mutation
127 #########################
128 warm = ['red', 'yellow', 'orange']
129 sortedwarm = warm.sort()
130 print(warm)
131 print(sortedwarm)
132
133 cool = ['grey', 'green', 'blue']
134 sortedcool = sorted(cool)
135 print(cool)
136 print(sortedcool)
137
138 #########################
139 ## EXAMPLE: lists of lists of lists...
140 #########################
141 warm = ['yellow', 'orange']
142 hot = ['red']
143 brightcolors = [warm]
144 brightcolors.append(hot)
145 print(brightcolors)
146 hot.append('pink')
147 print(hot)
148 print(brightcolors)
149
150
151 ###############################
152 ## EXAMPLE: mutating a list while iterating over it
153 ###############################
154 def remove_dups(L1, L2):
155 for e in L1:
156 if e in L2:
157 L1.remove(e)
158
159 def remove_dups_new(L1, L2):
160 L1_copy = L1[:]
161 for e in L1_copy:
162 if e in L2:
163 L1.remove(e)
164
165 L1 = [1, 2, 3, 4]
166 L2 = [1, 2, 5, 6]
167 remove_dups(L1, L2)
168 print(L1, L2)
169
170 L1 = [1, 2, 3, 4]
171 L2 = [1, 2, 5, 6]
172 remove_dups_new(L1, L2)
173 print(L1, L2)
174
175 ###############################
176 ## EXERCISE: Test yourself by predicting what the output is and
177 ## what gets mutated then check with the Python Tutor
178 ###############################
179 cool = ['blue', 'green']
180 warm = ['red', 'yellow', 'orange']
181 print(cool)
182 print(warm)
183
184 colors1 = [cool]
185 print(colors1)
186 colors1.append(warm)
187 print('colors1 = ', colors1)
188
189 colors2 = [['blue', 'green'],
190 ['red', 'yellow', 'orange']]
191 print('colors2 =', colors2)
192
193 warm.remove('red')
194 print('colors1 = ', colors1)
195 print('colors2 =', colors2)
196
197 for e in colors1:
198 print('e =', e)
199
200 for e in colors1:
201 if type(e) == list:
202 for e1 in e:
203 print(e1)
204 else:
205 print(e)
206
207 flat = cool + warm
208 print('flat =', flat)
209
210 print(flat.sort())
211 print('flat =', flat)
212
213 new_flat = sorted(flat, reverse = True)
214 print('flat =', flat)
215 print('new_flat =', new_flat)
216
217 cool[1] = 'black'
218 print(cool)
219 print(colors1)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.