-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeCountOrdered.py
39 lines (30 loc) · 1.03 KB
/
typeCountOrdered.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
# Output the items ordered by the greatest to lowest number of complaints.
#
# e.g. output:
# Street Condition with 450 complaints
# Highway Sign - Damaged with 421 complaints
# Sidewalk condition with 405 complaints
# Bridge condition with 388 complaints
import csv
import os
import sys
# Problem solution.
def solution(filename):
complaintTypeCount = {}
# Columns:
# Unique Key,Created Date,Closed Date,Agency,Agency Name,Complaint Type,...
with open(filename) as f:
csvReader = csv.reader(f)
headers = next(csvReader)
for row in csvReader:
complaintType = row[5]
count = complaintTypeCount.setdefault(complaintType, 0)
complaintTypeCount[complaintType] = count + 1
# Computes list of complaint types sorted by number of complaints.
sortedByCount =\
sorted(complaintTypeCount.iteritems(), key=lambda x: (-x[1], x[0]), reverse=False)
for item in sortedByCount:
print '%s with %d complaints' % (item[0], item[1])
if __name__ == '__main__':
# Runs with provided filename.
solution(sys.argv[1])