-
Notifications
You must be signed in to change notification settings - Fork 0
/
popular_names.py
70 lines (44 loc) · 1.34 KB
/
popular_names.py
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
def open_file(file):
f = open(file, 'r')
try:
max1 = 0
max2 = 0
name1 = None
name2 = None
names = []
for line in f:
row = line.split(',')
row = [row[0], row[1], int(row[2].replace('\n', ''))]
if row[1] == 'F':
names.append(row)
return sorted(names, key=lambda x: x[2])[-2]
print names
finally:
f.close()
print open_file('popular_names.txt')
#or
def open_file2(file):
f = open(file, 'r')
try:
max1 = 0
max2 = 0
name1 = None
name2 = None
names = []
for line in f:
row = line.split(',')
row = [row[0], row[1], int(row[2].replace('\n', ''))]
if row[1] == 'F':
if row[2] > max1:
max2 = max1
name2 = name1
max1 = row[2]
name1 = row[0]
elif row[2] > max2:
max2 = row[2]
name2 = row[0]
return name2, max2
print names
finally:
f.close()
print open_file2('popular_names.txt')