list1 = list(range(-20, 50, 5))
list2 = list(range(100, 130, 2))
list3 = [list1, list2]
print(list3) # Print new list
print(list3[0]) # Print list1
print(list3[1]) # Print list2
print(list3[0][1]) # Print second element in first list
[[-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45], [100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128]] [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45] [100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128] -15
Above, we created a table with rows. Using zip()
function we can loop through two or more lists at the same time. With that we can create a table with columns:
list4 = []
for i, j in zip(list1, list2):
row = [i, j]
list4. append(row)
print(list4)
# This can be also done with list comprehension
list5 = [[i, j] for i, j in zip(list1, list2)]
print(list5)
[[-20, 100], [-15, 102], [-10, 104], [-5, 106], [0, 108], [5, 110], [10, 112], [15, 114], [20, 116], [25, 118], [30, 120], [35, 122], [40, 124], [45, 126]] [[-20, 100], [-15, 102], [-10, 104], [-5, 106], [0, 108], [5, 110], [10, 112], [15, 114], [20, 116], [25, 118], [30, 120], [35, 122], [40, 124], [45, 126]]
(nested_lists_exercises)=
import random
# A seed prints the same sequence of numbers every time
# USE FOR TESTING ONLY
# Delete the following line, when done
random.seed(1)
a = 10
i_map=[[random.randint(100,500) for i in range(a)] for j in range(a)]
for i in range(a):
print(*i_map[i])
168 391 491 132 230 160 353 489 330 341 433 294 207 148 349 114 299 321 411 490 492 101 456 328 236 469 217 402 152 262 115 111 113 432 377 104 295 451 210 316 471 114 370 213 491 324 353 383 219 276 218 446 212 489 335 248 111 313 384 428 151 195 422 470 251 161 480 270 469 464 356 316 359 443 197 255 245 400 355 358 301 401 117 345 224 480 306 312 440 188 287 380 459 497 445 477 291 144 324 439
The numbers in the grid represent the height of a particulair coordinate. The UFO can land only on a local peak i.e. a point surrounded by the points of equal-or-lower height, e.g
1 2 3
5 9 6
6 9 4
The middle cell with $9$ inside satisfies the local peak condition.
Return a list of coordinates with local peaks, do not consider the edge cells (but use them in determining other peaks). Good luck General!
{admonition}
:class: dropdown
```python
import random
random.seed(1)
a = 10
i_map=[[random.randint(100,500) for i in range(a)] for j in range(a)]
coords = []
#Scanning through the grid
for i in range(1,a-1):
for j in range(1,a-1):
#Checking the peak condition
success = True
for x in range(i-1,i+2):
for y in range(j-1,j+2):
if i_map[x][y] > i_map[i][j]:
success = False
#Found a peak
if success:
coords.append([i,j])
print(coords)
```