In [1]:
# Boston Marathon - fĂ¸lgende eksempel er en bearbeidet versjon av kode fra kap. 17.1 i
# Guttag, J,V, 2016: Introduction to computation and programming using Python
#
# Read the contents of the given file. Assumes the file
# in a comma-separated format, with 6 elements in each entry:
# 0. Name (string),
# 1. Gender (string)
# 2. Age (int)
# 3. Division (int),
# 4. Country (string),
# 5. Overall time (float)
#

import matplotlib.pyplot as plt

data = {
'name': [],
'gender': [],
'age': [],
'division': [],
'country': [],
'time': []
}

with open('bm_results2012.txt') as f:

print(type(lines))
print(lines[0])
print(len(lines))

lnr = 0

for line in lines:
lnr += 1
split = line.split(',')
if lnr == 1:
print(split)
data['name'].append(split[0])
data['gender'].append(split[1])
data['age'].append(int(split[2]))
data['division'].append(int(split[3]))
data['country'].append(split[4])
data['time'].append(float(split[5]))
if lnr == 2:
print(data)

<class 'list'>
"Gebremariam Gebregziabher",M,27,14,ETH,142.93

21541
['"Gebremariam Gebregziabher"', 'M', '27', '14', 'ETH', '142.93\n']
{'name': ['"Gebremariam Gebregziabher"', '"Matebo Levy"'], 'gender': ['M', 'M'], 'age': [27, 22], 'division': [14, 2], 'country': ['ETH', 'KEN'], 'time': [142.93, 133.1]}

In [2]:
plt.hist(data['time'], 20, edgecolor='black')
plt.title('2012 Boston Marathon')
plt.xlabel('Minutes to Complete Race')
plt.ylabel('Number of Runners')
plt.show()