-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrank.py
51 lines (39 loc) · 1010 Bytes
/
rank.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
import csv
import collections
CityStarbucks = collections.namedtuple(
"CityStarbucks", ["country", "capital", "n_starbucks"]
)
def read_data():
result = []
with open("data.csv", "r") as f:
reader = csv.reader(f, delimiter=",", quotechar='"')
for row in reader:
result.append(
CityStarbucks._make(
[
row[0],
row[1],
int(row[2]),
]
)
)
return result
def calculcate_zeros(data):
return len(
[
elem for elem in data
if elem.n_starbucks != 0
]
)
def top(data, n=30):
sorted_data = sorted(
data, key=lambda e: e.n_starbucks, reverse=True
)
return [elem for elem in sorted_data][:n]
def main():
data = read_data()
print(calculcate_zeros(data))
for elem in top(data):
print(elem)
if __name__ == "__main__":
main()