0
這裏是我的代碼: 創建一個類,rit_object是一個私有的類比對參數具有類型:字典映射蟒蛇
class YearCount(rit_object):
__slots__ = ('year', 'count')
_types = (int, int)
返回YearCount對象:
def createYearCount(year, count):
return YearCount(year, count)
通讀文件。輸出應該類似於:
import wordData
words = wordData.readWordFile(’very_short.csv’)
print(words)
{’airport’: [YearCount(year=2007, count=175702), YearCount(year=2008,
count=173294)], ’wandered’: [YearCount(year=2005, count=83769),
YearCount(year=2006, count=87688), YearCount(year=2007, count=108634),
YearCount(year=2008, count=171015)], ’request’: [YearCount(year=2005,
count=646179), YearCount(year=2006, count=677820), YearCount(year=2007,
count=697645), YearCount(year=2008, count=795265)]}
readWordFile(文件名):
def readWordFile(fileName):
#read in the entire unigram dataset
words = {}
for line in fileName:
new = line.split(', ')
print(new)
id = new[0]
print(id)
yc = createYearCount(int(new[1]), int(new[2]))
# add to list or create a new list
if not id in words:
words[id] = [yc]
else:
words[id].append(yc)
print(words)
如果從我的readWordFile我總出現用途「字」,是我totaloccurences功能corrctly工作對生產總數每年?
def totalOccurences(word, words):
count = 0
if words[id] == word:
count += YearCount.count
return count
文本文件:
airport, 2007, 175702
airport, 2008, 173294
request, 2005, 646179
request, 2006, 677820
request, 2007, 697645
request, 2008, 795265
wandered, 2005, 83769
wandered, 2006, 87688
wandered, 2007, 108634
wandered, 2008, 171015