-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_00_intermediate.py
More file actions
94 lines (70 loc) · 2.03 KB
/
lesson_00_intermediate.py
File metadata and controls
94 lines (70 loc) · 2.03 KB
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
# FOR LOOPS AND BASIC LIST COMPREHENSIONS
import csv
import requests
nums = range(1, 6)
for num in nums:
print(num)
# for loop to create a list of cubes
cubes = []
for num in nums:
cubes.append(num**3)
print(cubes)
# equivalent list comprehension
cubes = [num ** 3 for num in nums]
print(cubes)
# LIST COMPRENSION WITH CONDITIONS
nums = range(1, 6)
# for loop to create a list of cubes of even numbes
cubes_of_even = []
for num in nums:
if num % 2 == 0:
cubes_of_even.append(num**3)
# equivalent list comprehension
cubes_of_even = [num ** 3 for num in nums if num % 2 == 0]
print(cubes_of_even)
# dictionairies
family = {'dad': 'homer', 'mom': 'marge', 'size': 6}
print(family['dad'])
print(len(family))
print(family.keys())
print(family.values())
print(family.items())
with open('2014-average-ticket-price.csv', 'rU') as f:
data = [row for row in csv.reader(f)] # list of lists
# examine the data
type(data)
print(len(data))
print(data[0])
print(data[1])
# save the data we want
data = data[1:97]
# step 1: create a list that only contains events
events = [row[0] for row in data]
print(events)
# step 2: create a list tht only contains prices (stored as integer)
prices = [int(row[2]) for row in data]
print(prices)
# step 3: figure out how to locate the away teams
print(events[0])
print(events[0].find('at'))
stop = events[0].find('at')
print(events[0][:stop])
# step 4: use a for loop to make a list of the away teams
away_teams = []
for event in events:
stop = event.find('at')
away_teams.append(event[:stop])
print(away_teams)
# step 5: use a for loop to make a list of the home teams
home_teams = []
for event in events:
start = event.find(' at ') + 4
stop = event.find(' Tickets ')
home_teams.append(event[start:stop])
print(home_teams)
# step 6: figure out how to get prices only for Ravens home games
zip(home_teams, prices)
Raven_home = [price for team, price in zip(
home_teams, prices) if team == 'Baltimore Ravens']
print(Raven_home)
print(float(sum(Raven_home)) / len(Raven_home))