-
Notifications
You must be signed in to change notification settings - Fork 0
/
potholes.py
64 lines (50 loc) · 1.45 KB
/
potholes.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
#!/usr/bin/python2.7
import csv
potholes_by_block = {}
def make_block(address):
'''
Rewrite an address to strip address to 1000's (10 blocks\
)'''
parts = address.split()
# For a number like '5412' this makes '5XXX'
parts[0] = parts[0][:-3] + 'XXX'
return ' '.join(parts)
f = open('potholes.csv')
for row in csv.DictReader(f):
status = row['STATUS']
if status == 'Open':
addr = row['STREET ADDRESS']
num = row['NUMBER OF POTHOLES FILLED ON BLOCK']
# Tabulate
block = make_block(addr)
if block not in potholes_by_block:
#This is the first occurence of given adress \
potholes_by_block[block] = 1
else:
potholes_by_block[block] += 1
# Problem: how to sort, find most potholes?
num_potholes_block = []
for key in potholes_by_block.keys():
num_potholes_block.append((potholes_by_block[key],
key))
num_potholes_block.sort(reverse=True )
for num, block in num_potholes_block[:10]:
print num, block
# other thing written on the video
# python2.7 mode
# >>> f = open('potholes.csv')
# >>> import csv
# >>> r = csv.DictReader(f)
# >>> next(r)
# >>> nums = [1, 3, 5, 7]
# >>> squares = []
# >>> for x in nums:
# ... squares.append(x*x)
# ...
# >>> squares
# [1, 9, 25, 49]
# the same code
# >>> squares = [x*x for x in nums]
# >>> squares
# [1, 9, 25, 49]
# slide link http://www.slideshare.net/dabeaz/an-introduction-to-python-concurrencyk