Module 6: Semi structured data to tables

Creating dictionaries to represent data at different aggregations

Differing data representations allow us to group data according to different arrangements. This is different from just aggregarion where we are asking for summary information and often collapsing rows.

Using for loops to look ahead and look back:

  • Sometimes you need to use a prior value

  • Classically, you can see this within min and max functions.

Use enumerate() to get the index and the content at the same time

import string

letters = list(string.ascii_uppercase)

for i, l in enumerate(letters):
    print(letters[i-1], i)
Z 0
A 1
B 2
C 3
D 4
E 5
F 6
G 7
H 8
I 9
J 10
K 11
L 12
M 13
N 14
O 15
P 16
Q 17
R 18
S 19
T 20
U 21
V 22
W 23
X 24
Y 25

Skip the first value to as your base case

This pattern allows you to look back by one element, but requires that you skip the first one since there isn't anything prior.

for i, l in enumerate(letters):
    if i == 0:
        continue
    print(letters[i-1], l)
A B
B C
C D
D E
E F
F G
G H
H I
I J
J K
K L
L M
M N
N O
O P
P Q
Q R
R S
S T
T U
U V
V W
W X
X Y
Y Z

while loops

Can be good for this because you are usually messing with the index values at the same time.

There are many ways to use these, but you can do it with a sentinel value that you directly flip from True to False. The while loop will run so long as the boolean value it is evaluating evaluates to True. Being careful with your variable names is key to help you keep the values and logic straight.

nums = [4,89,7,56,7,9,0,8,6,5,9,-1] # get your data

#establish a base case
not_seen = True # must be True to start with to get the loop to run

# you also need to take care of the iteration by hand

index = 0 

# now, think of the True case until it's true. What is it?
# until a thing is seen, do this loop.
# it's usually weird, like "while I have not seen this thing"

while not_seen:
    n = nums[index]
    if n == 9:
        not_seen = False
        print("found!")
    else:
        index += 1

So when is a while loop really necessary?

When you don't know how many times you need to run something. This usually happens when you are working with interactive programs (e.g. games) and simulations. There are plenty of other cases, like certain algorithms, etc. as well. We'll get into that later.

Last updated