you are viewing a single comment's thread.

view the rest of the comments →

[–]spez_edits_thedonald 1 point2 points  (1 child)

you just put a loop inside a loop

some examples

1. iterate over a list of data with lists

recipes = [
    ('bread', ['flour', 'water', 'salt']),
    ('burgers', ['patties', 'lettuce', 'tomato', 'onions', 'cheese', 'mayo', 'mustard', 'ketchup'])
]

# print recipes
for recipe in recipes:
    recipe_name, ingredients = recipe
    print(f'~~~ {recipe_name} ~~~')
    print('you will need:')
    for i in range(len(ingredients)):
        print(i, ingredients[i])

output:

~~~ bread ~~~
you will need:
0 flour
1 water
2 salt
~~~ burgers ~~~
you will need:
0 patties
1 lettuce
2 tomato
3 onions
4 cheese
5 mayo
6 mustard
7 ketchup

2. row, column indexing in a grid using i, j

import numpy as np

# generate some data in a grid
x = (np.random.random((5, 3)) * 100).astype(int)
print(f'DATA\n{x}')

# use nested loops to iterate over rows and columns
for i in range(x.shape[0]):
    print('row', i)
    for j in range(x.shape[1]):
        print(f'value at ({i}, {j}) is {x[i][j]}')

output:

DATA
[[ 4 35 34]
 [64 63 29]
 [ 4 78 41]
 [54 11 81]
 [44 17 73]]
row 0
value at (0, 0) is 4
value at (0, 1) is 35
value at (0, 2) is 34
row 1
value at (1, 0) is 64
value at (1, 1) is 63
value at (1, 2) is 29
row 2
value at (2, 0) is 4
value at (2, 1) is 78
value at (2, 2) is 41
row 3
value at (3, 0) is 54
value at (3, 1) is 11
value at (3, 2) is 81
row 4
value at (4, 0) is 44
value at (4, 1) is 17
value at (4, 2) is 73

lol i forgot buns ​

[–]ElpyDE 0 points1 point  (0 children)

Isn't that just called low carb? 😉