-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lists.py
338 lines (269 loc) · 12 KB
/
test_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
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
"""Lists.
# @see: https://www.learnpython.org/en/Lists
# @see: https://docs.python.org/3/tutorial/introduction.html
# @ee: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
Python knows a number of compound data types, used to group together
other values. The most versatile is the list, which can be written as a
list of comma-separated values (items) between square brackets. Lists
might contain items of different types, but usually the items all have
the same type.
"""
import pytest
def test_list_type():
"""List type."""
# Lists are very similar to arrays. They can contain any type of variable, and they can contain
# as many variables as you wish. Lists can also be iterated over in a very simple manner.
# Here is an example of how to build a list.
squares = [1, 4, 9, 16, 25]
assert isinstance(squares, list)
# Like strings (and all other built-in sequence type), lists can be
# indexed and sliced:
assert squares[0] == 1 # indexing returns the item
assert squares[-1] == 25
assert squares[-3:] == [9, 16, 25] # slicing returns a new list
# All slice operations return a new list containing the requested elements.
# This means that the following slice returns a new (shallow) copy of
# the list:
assert squares[:] == [1, 4, 9, 16, 25]
# Lists also support operations like concatenation:
assert squares + [36, 49, 64, 81, 100] == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# Unlike strings, which are immutable, lists are a mutable type, i.e. it
# is possible to change their content:
cubes = [1, 8, 27, 65, 125] # something's wrong here, the cube of 4 is 64!
cubes[3] = 64 # replace the wrong value
assert cubes == [1, 8, 27, 64, 125]
# You can also add new items at the end of the list, by using
# the append() method
cubes.append(216) # add the cube of 6
cubes.append(7 ** 3) # and the cube of 7
assert cubes == [1, 8, 27, 64, 125, 216, 343]
# Assignment to slices is also possible, and this can even change the size
# of the list or clear it entirely:
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
letters[2:5] = ['C', 'D', 'E'] # replace some values
assert letters == ['a', 'b', 'C', 'D', 'E', 'f', 'g']
letters[2:5] = [] # now remove them
assert letters == ['a', 'b', 'f', 'g']
# clear the list by replacing all the elements with an empty list
letters[:] = []
assert letters == []
# The built-in function len() also applies to lists
letters = ['a', 'b', 'c', 'd']
assert len(letters) == 4
# It is possible to nest lists (create lists containing other lists),
# for example:
list_of_chars = ['a', 'b', 'c']
list_of_numbers = [1, 2, 3]
mixed_list = [list_of_chars, list_of_numbers]
assert mixed_list == [['a', 'b', 'c'], [1, 2, 3]]
assert mixed_list[0] == ['a', 'b', 'c']
assert mixed_list[0][1] == 'b'
def test_list_methods():
"""Test list methods."""
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
# list.append(x)
# Add an item to the end of the list.
# Equivalent to a[len(a):] = [x].
fruits.append('grape')
assert fruits == ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'grape']
# list.remove(x)
# Remove the first item from the list whose value is equal to x.
# It raises a ValueError if there is no such item.
fruits.remove('grape')
assert fruits == ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
with pytest.raises(Exception):
fruits.remove('not existing element')
# list.insert(i, x)
# Insert an item at a given position. The first argument is the index of the element
# before which to insert, so a.insert(0, x) inserts at the front of the list,
# and a.insert(len(a), x) is equivalent to a.append(x).
fruits.insert(0, 'grape')
assert fruits == ['grape', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
# list.index(x[, start[, end]])
# Return zero-based index in the list of the first item whose value is equal to x.
# Raises a ValueError if there is no such item.
# The optional arguments start and end are interpreted as in the slice notation and are used
# to limit the search to a particular subsequence of the list. The returned index is computed
# relative to the beginning of the full sequence rather than the start argument.
assert fruits.index('grape') == 0
assert fruits.index('orange') == 1
assert fruits.index('banana') == 4
assert fruits.index('banana', 5) == 7 # Find next banana starting a position 5
with pytest.raises(Exception):
fruits.index('not existing element')
# list.count(x)
# Return the number of times x appears in the list.
assert fruits.count('tangerine') == 0
assert fruits.count('banana') == 2
# list.copy()
# Return a shallow copy of the list. Equivalent to a[:].
fruits_copy = fruits.copy()
assert fruits_copy == ['grape', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
# list.reverse()
# Reverse the elements of the list in place.
fruits_copy.reverse()
assert fruits_copy == [
'banana',
'apple',
'kiwi',
'banana',
'pear',
'apple',
'orange',
'grape',
]
# list.sort(key=None, reverse=False)
# Sort the items of the list in place (the arguments can be used for sort customization,
# see sorted() for their explanation).
fruits_copy.sort()
assert fruits_copy == [
'apple',
'apple',
'banana',
'banana',
'grape',
'kiwi',
'orange',
'pear',
]
# list.pop([i])
# Remove the item at the given position in the list, and return it. If no index is specified,
# a.pop() removes and returns the last item in the list. (The square brackets around the i in
# the method signature denote that the parameter is optional, not that you should type square
# brackets at that position.)
assert fruits == ['grape', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
assert fruits.pop() == 'banana'
assert fruits == ['grape', 'orange', 'apple', 'pear', 'banana', 'kiwi', 'apple']
# list.clear()
# Remove all items from the list. Equivalent to del a[:].
fruits.clear()
assert fruits == []
def test_del_statement():
"""The del statement
There is a way to remove an item from a list given its index instead of its value: the del
statement. This differs from the pop() method which returns a value. The del statement can also
be used to remove slices from a list or clear the entire list (which we did earlier by
assignment of an empty list to the slice).
"""
numbers = [-1, 1, 66.25, 333, 333, 1234.5]
del numbers[0]
assert numbers == [1, 66.25, 333, 333, 1234.5]
del numbers[2:4]
assert numbers == [1, 66.25, 1234.5]
del numbers[:]
assert numbers == []
# del can also be used to delete entire variables:
del numbers
with pytest.raises(Exception):
# Referencing the name a hereafter is an error (at least until another
# value is assigned to it).
assert numbers == [] # noqa: F821
def test_list_comprehensions():
"""List Comprehensions.
List comprehensions provide a concise way to create lists. Common applications are to make new
lists where each element is the result of some operations applied to each member of another
sequence or iterable, or to create a subsequence of those elements that satisfy a certain
condition.
A list comprehension consists of brackets containing an expression followed by a for clause,
then zero or more for or if clauses. The result will be a new list resulting from evaluating
the expression in the context of the for and if clauses which follow it.
"""
# For example, assume we want to create a list of squares, like:
squares = []
for number in range(10):
squares.append(number ** 2)
assert squares == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# Note that this creates (or overwrites) a variable named "number" that still exists after
# the loop completes. We can calculate the list of squares without any side effects using:
squares = list(map(lambda x: x ** 2, range(10)))
assert squares == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# or, equivalently (which is more concise and readable):
squares = [x ** 2 for x in range(10)]
assert squares == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# For example, this listcomp combines the elements of two lists if they are not equal.
combinations = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
assert combinations == [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
# and it’s equivalent to:
combinations = []
for first_number in [1, 2, 3]:
for second_number in [3, 1, 4]:
if first_number != second_number:
combinations.append((first_number, second_number))
assert combinations == [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
# Note how the order of the for and if statements is the same in both these snippets.
# If the expression is a tuple (e.g. the (x, y) in the previous example),
# it must be parenthesized.
# Let's see some more examples:
vector = [-4, -2, 0, 2, 4]
# Create a new list with the values doubled.
doubled_vector = [x * 2 for x in vector]
assert doubled_vector == [-8, -4, 0, 4, 8]
# Filter the list to exclude negative numbers.
positive_vector = [x for x in vector if x >= 0]
assert positive_vector == [0, 2, 4]
# Apply a function to all the elements.
abs_vector = [abs(x) for x in vector]
assert abs_vector == [4, 2, 0, 2, 4]
# Call a method on each element.
fresh_fruit = [' banana', ' loganberry ', 'passion fruit ']
clean_fresh_fruit = [weapon.strip() for weapon in fresh_fruit]
assert clean_fresh_fruit == ['banana', 'loganberry', 'passion fruit']
# Create a list of 2-tuples like (number, square).
square_tuples = [(x, x ** 2) for x in range(6)]
assert square_tuples == [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
# Flatten a list using a listcomp with two 'for'.
vector = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flatten_vector = [num for elem in vector for num in elem]
assert flatten_vector == [1, 2, 3, 4, 5, 6, 7, 8, 9]
def test_nested_list_comprehensions():
"""Nested List Comprehensions
The initial expression in a list comprehension can be any arbitrary expression, including
another list comprehension.
"""
# Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4:
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]
# The following list comprehension will transpose rows and columns:
transposed_matrix = [[row[i] for row in matrix] for i in range(4)]
assert transposed_matrix == [
[1, 5, 9],
[2, 6, 10],
[3, 7, 11],
[4, 8, 12],
]
# As we saw in the previous section, the nested listcomp is evaluated in the context of the
# for that follows it, so this example is equivalent to:
transposed = []
for i in range(4):
transposed.append([row[i] for row in matrix])
assert transposed == [
[1, 5, 9],
[2, 6, 10],
[3, 7, 11],
[4, 8, 12],
]
# which, in turn, is the same as:
transposed = []
for i in range(4):
# the following 3 lines implement the nested listcomp
transposed_row = []
for row in matrix:
transposed_row.append(row[i])
transposed.append(transposed_row)
assert transposed == [
[1, 5, 9],
[2, 6, 10],
[3, 7, 11],
[4, 8, 12],
]
# In the real world, you should prefer built-in functions to complex flow statements.
# The zip() function would do a great job for this use case:
assert list(zip(*matrix)) == [
(1, 5, 9),
(2, 6, 10),
(3, 7, 11),
(4, 8, 12),
]