到目前为止,这是我的代码: * rit_object是一个私有类,为每个参数分配类型
from rit_object import *
class YearCount(rit_object):
__slots__ = ( 'year', 'count')
_types = (int, int)
def createYearCount(year, count):
return YearCount(year, count)
def readWordFile(fileName):
#read in the entire unigram dataset
for line in fileName:
new = line.split(', ')
print(new)
#s = createYearCount(int(line[1]), line[2])
#print(s)
def main():
fileName = input('Enter filename: ')
readWordFile(open(fileName))
main()
我必须在一个csv文件中读取,该文件代表该年中该单词,年份和该单词的出现次数:
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
我的readWordFile函数应该输入一个fileName并输出一个字典映射单词到YearCount对象的列表。输出应该如下:
{’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 )]}
我不确定如何使用字典映射。如何创建包含单词的字典作为键,值是包含年份和计数的YearCount对象。?
答案 0 :(得分:1)
首先你应该有一个dict obj
result = {}
然后,在函数readWordFile
new = line.split(', ')
id = new[0]
yc = createYearCount(int(new[1]), new[2])
# add to list or create a new list
if (not result.has_key(id)):
result[id] = [yc]
else:
result[id].append(yc)