CSc 110
2D Lists
Benjamin Dicken
2D Lists!
Review
Should be familiar with this kind of list usage:
items = [5, 10, 20, 6, 7, 8]
items[0] = 10
items[1] = 3
items[4] = 99
print(str(items))
index | 0 | 1 | 2 | 3 | 4 | 5 |
value | 10 | 3 | 20 | 6 | 99 | 8 |
2D list
items = [[9, 8, 7, 8], [10, 20, 30, 4], [5, 50, 55, 4]]
idxs | 0 | 1 | 2 | 3 |
0 | 9 | 8 | 7 | 8 |
1 | 10 | 20 | 30 | 4 |
2 | 5 | 50 | 55 | 4 |
2D list
items = [[9, 8, 7, 8],
[10, 20, 30, 4],
[5, 50, 55, 4]]
idxs | 0 | 1 | 2 | 3 |
0 | 9 | 8 | 7 | 8 |
1 | 10 | 20 | 30 | 4 |
2 | 5 | 50 | 55 | 4 |
2D list
What will each of the below list accesses evaluate to?
val_a = items[0][0]
val_b = items[2][3]
val_c = items[1][2]
print(val_a, val_b, val_c)
items = [[9, 8, 7, 8],
[10, 20, 30, 4],
[5, 50, 55, 4]]
idxs | 0 | 1 | 2 | 3 |
0 | 9 | 8 | 7 | 8 |
1 | 10 | 20 | 30 | 4 |
2 | 5 | 50 | 55 | 4 |
Activity
Which lookups are valid?
val_a = items[1][5] # ?
val_b = items[2][5] # ?
val_c = items[3][2] # ?
items = [[9, 7, 8],
[1, 2, 3, 4, 5, 6],
[5],
[10, 20, 30]]
idxs | 0 | 1 | 2 | 3 | 4 | 5 |
0 | 9 | 7 | 8 | | | |
1 | 1 | 2 | 3 | 4 | 5 | 6 |
2 | 5 | | | | | |
3 | 10 | 20 | 30 | | | |
Activity
Which lookups are valid?
val_a = items[1][5] # OK
val_b = items[2][5] # NOT OK
val_c = items[3][2] # OK
items = [[9, 7, 8],
[1, 2, 3, 4, 5, 6],
[5],
[10, 20, 30]]
idxs | 0 | 1 | 2 | 3 | 4 | 5 |
0 | 9 | 7 | 8 | | | |
1 | 1 | 2 | 3 | 4 | 5 | 6 |
2 | 5 | | | | | |
3 | 10 | 20 | 30 | | | |
What will it print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
l1 = names[1][0][2]
l2 = names[2][1][2]
l3 = names[0][1][4]
print(l1 + l2 + l3)
Activity
2D list
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
names ==
names[2] ==
names[2][1] ==
names[2][1][0] ==
[["Ric", "Janet", "Joe"], ["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
["Cam", "Jan", "Jane"]
"Jan"
"J"
Traversing a 2d List
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
print(i)
Activity
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
print(i)
['Ric', 'Janet', 'Joe']
['Ike', 'Alan', 'Ko']
['Cam', 'Jan', 'Jane']
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in names:
print(i)
Activity
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in names:
print(i)
['Ric', 'Janet', 'Joe']
['Ric', 'Janet', 'Joe']
['Ric', 'Janet', 'Joe']
['Ike', 'Alan', 'Ko']
['Ike', 'Alan', 'Ko']
['Ike', 'Alan', 'Ko']
['Cam', 'Jan', 'Jane']
['Cam', 'Jan', 'Jane']
['Cam', 'Jan', 'Jane']
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in names:
print(j)
Activity
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in names:
print(j)
['Ric', 'Janet', 'Joe']
['Ike', 'Alan', 'Ko']
['Cam', 'Jan', 'Jane']
['Ric', 'Janet', 'Joe']
['Ike', 'Alan', 'Ko']
['Cam', 'Jan', 'Jane']
['Ric', 'Janet', 'Joe']
['Ike', 'Alan', 'Ko']
['Cam', 'Jan', 'Jane']
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in i:
print(j)
Activity
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in i:
print(j)
Ric
Janet
Joe
Ike
Alan
Ko
Cam
Jan
Jane
What would you change?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in i:
print(j)
Ric Janet Joe
Ike Alan Ko
Cam Jan Jane
Ric
Janet
Joe
Ike
Alan
Ko
Cam
Jan
Jane
Activity
What would you change?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in names:
for j in i:
print(j + '\t', end='')
print()
Ric Janet Joe
Ike Alan Ko
Cam Jan Jane
Ric
Janet
Joe
Ike
Alan
Ko
Cam
Jan
Jane
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in range(0, len(names)):
for j in range(0, len(names[i])):
print(names[i][j], end="\t")
print()
Activity
What will this print?
names = [["Ric", "Janet", "Joe"],
["Ike", "Alan", "Ko"],
["Cam", "Jan", "Jane"]]
for i in range(0, len(names)):
for j in range(0, len(names[i])):
print(names[-j-1][i], end="\t")
print()
Activity
CSV
CSV Formatting
CSV Grades example
Name,SID,exam1,exam2,final
Steve,1429,67,84,91
Annie,3211,81,85,85
James,3171,90,75,80
Carlita,7877,85,87,89
CSV Grades example (students.csv)
Name,SID,exam1,exam2,final
Steve,1429,67,84,91
Annie,3211,81,85,85
James,3171,90,75,80
Carlita,7877,85,87,89
Optional row of column titles
Each row represents a row of data (an entry in the student table
Each row has the same number of columns - to match the titles
CSV Grades example (students.csv)
Steve,1429,67,84,91
Annie,3211,81,85,85
James,3171,90,75,80
Carlita,7877,85,87,89
Title row is optional
Each row represents a row of data (an entry in the student table
Each row has the same number of columns - to match the titles
Load the contents of csv file into list
csv_data = []
# What should go here?
1,4,7,2
3,10,20,30
3,4,7,1
1,1,1,2
1,5,5,1
0,1,0,1
numbers.csv
Activity
Load the contents of csv file into list
csv_data = []
csv_file = open('numbers.csv', 'r')
# What should go here?
1,4,7,2
3,10,20,30
3,4,7,1
1,1,1,2
1,5,5,1
0,1,0,1
numbers.csv
Load the contents of csv file into list
csv_data = []
csv_file = open('numbers.csv', 'r')
for line in csv_file:
line = line.strip('\n')
values = line.split(',')
# What should go here?
1,4,7,2
3,10,20,30
3,4,7,1
1,1,1,2
1,5,5,1
0,1,0,1
numbers.csv
Load the contents of csv file into list
csv_data = []
csv_file = open('numbers.csv', 'r')
for line in csv_file:
line = line.strip('\n')
values = line.split(',')
csv_data.append(values)
1,4,7,2
3,10,20,30
3,4,7,1
1,1,1,2
1,5,5,1
0,1,0,1
numbers.csv
Load the contents of csv file into list
csv_data = []
csv_file = open('numbers.csv', 'r')
for line in csv_file:
line = line.strip('\n')
values = line.split(',')
csv_data.append(values)
1,4,7,2
3,10,20,30
3,4,7,1
1,1,1,2
1,5,5,1
0,1,0,1
numbers.csv
What type are the values in the 2D list?
Activity
Load the contents of csv file into list
csv_data = []
csv_file = open('numbers.csv', 'r')
for line in csv_file:
line = line.strip('\n')
values = line.split(',')
num_vals = []
for i in values:
num_vals.append( int(i) )
csv_data.append(num_vals)
1,4,7,2
3,10,20,30
3,4,7,1
1,1,1,2
1,5,5,1
0,1,0,1
numbers.csv
Flip the order U/D
csv_data = []
## Assume that the data has
## already been read in to
## csv_data
# (1) First, flip the rows
# (2) Then, print them out
1,4,7,2
1,5,5,1
0,1,0,1
numbers.csv
0 1 0 1
1 5 5 1
1 4 7 2
output
Activity
Flip the order U/D
csv_data = []
flipped = []
for i in range(0, len(csv_data)):
ni = -1 - i
flipped.append( csv_data[ni] )
# (2) Then, print them out
1,4,7,2
1,5,5,1
0,1,0,1
numbers.csv
0 1 0 1
1 5 5 1
1 4 7 2
output
Flip the order U/D
csv_data = []
flipped = []
for i in csv_data:
flipped.insert(0, i)
# (2) Then, print them out
1,4,7,2
1,5,5,1
0,1,0,1
numbers.csv
0 1 0 1
1 5 5 1
1 4 7 2
output
Flip the order U/D
csv_data = []
flipped = []
for i in csv_data:
flipped.insert(0, i)
for i in flipped:
for j in i:
print(str(j) + ' ', end='')
print()
1,4,7,2
1,5,5,1
0,1,0,1
numbers.csv
0 1 0 1
1 5 5 1
1 4 7 2
output
Graphical Stock Plotter
Graphical Stock Plotter
red,apple,100,125,120,95,130,145,170,130,150,177,230,260
blue,exxon,60,67,72,81,80,72,68,64,64,70,72,68
green,toyota,20,27,17,32,50,58,67,82,85,90,99,105
stocks.csv
Graphical Stock Plotter
red,apple,100,125,120,95,130,145,170,130,150,177,230,260
blue,exxon,60,67,72,81,80,72,68,64,64,70,72,68
green,toyota,20,27,17,32,50,58,67,82,85,90,99,105
stocks.csv
* Download plot.py
* Implement main
Activity
from graphics import graphics
def get_stock_data():
f = open('stocks.csv', 'r')
data = []
for line in f:
row = []
sp = line.split(',')
row.append(sp[0])
row.append(sp[1])
for element in sp[2:]:
row.append(int(element))
data.append(row)
return data
def main():
stock_data = get_stock_data()
gui = graphics(600, 300, 'stocks')
i = 0
while i < len(stock_data):
print(stock_data[i])
color = stock_data[i][0]
# TODO: Draw the label
j= 0
while j < len(stock_data[i])-2:
x = 10 + (j*50)
y = -(stock_data[i][j+2]-300)
# TODO: Draw the line
# TODO: Draw the dot
j += 1
i += 1
main()
from graphics import graphics
def get_stock_data():
f = open('stocks.csv', 'r')
data = []
for line in f:
row = []
sp = line.split(',')
row.append(sp[0])
row.append(sp[1])
for element in sp[2:]:
row.append(int(element))
data.append(row)
return data
def main():
stock_data = get_stock_data()
gui = graphics(600, 300, 'stocks')
i = 0
while i < len(stock_data):
print(stock_data[i])
color = stock_data[i][0]
gui.text(5, 5+30*i, stock_data[i][1], color, 25)
j= 0
prev_x = -1
prev_y = -1
while j < len(stock_data[i])-2:
x = 10 + (j*50)
y = -(stock_data[i][j+2]-300)
if prev_x >= 0:
gui.line(prev_x, prev_y, x, y, color, 2)
gui.ellipse(x, y, 10, 10, color)
prev_x = x
prev_y = y
j += 1
i += 1
main()