Skip to content

Commit 61a8864

Browse files
committed
taking out use of defaultdicts
1 parent bdcebd8 commit 61a8864

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

analyze_penn.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4-
import os, re
5-
from collections import defaultdict
4+
import os
5+
import re
66

77
findhours = re.compile(r'(\d{1,2}(:\d\d)?)-((\d{1,2}(:\d\d)?)(([AP]M)|NOON))')
88
starts = []
@@ -26,9 +26,17 @@
2626
print "found %(count)d total class times" % { "count": len(starts) }
2727

2828
# dividing up the classes we've found into different timeslots to count them
29-
timeslots = defaultdict(int)
29+
timeslots = {} #defaultdict(int)
3030
for i in range(len(starts)):
31-
timeslots[starts[i]+"-"+ends[i]] += 1
31+
key = starts[i]+"-"+ends[i]
32+
if timeslots.has_key(key):
33+
timeslots[key] += 1
34+
else:
35+
timeslots[key] = 1
36+
37+
38+
sorted_timeslots = sorted(timeslots.items(), key=lambda x:x[1], reverse=True)
39+
for time in sorted_timeslots:
40+
print time
41+
3242

33-
#print timeslots.items()
34-
print(sorted(timeslots.items(), key=lambda x:x[1], reverse=True)[:10])

sample4.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,7 @@
1515

1616
# the below line will cause an error if you uncomment it, because the dictionary doesn't have a key '6-9PM' in it
1717
# d['6-9PM'] += 1
18+
#
19+
# test if a dictionary has a key
20+
# d.has_key('6-9PM')
1821

19-
20-
# we'll use a default dictionary instead, which has a default value
21-
# that means we can add 1 to any key in it, even if it doesn't exist yet
22-
# because the dictionary will take any unknown key and give it the default integer value of 0
23-
24-
# this is a different type of import statement
25-
from collections import defaultdict
26-
27-
dd = defaultdict(int)
28-
print dd
29-
dd['6-9PM'] += 1
30-
print dd

sample5.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ def myfunction():
77
print "Hello World"
88

99
myfunction()
10+
#myfunction()

0 commit comments

Comments
 (0)