2012-11-08 22 views
-1

對於我的家庭作業,我需要編寫這個函數的主體,使用註釋作爲我的需求。在評論中提到的其他功能已經提供給我:Python函數從文本文件構建列表

def eventfreq(year,month): 

''' 
    Read DOT1000.txt and return a list of (d,ct) 
    pairs, where d is a date object of the form 
    datetime.date(A,B,C) 
    having A equal to the year argument and 
    B equal to the month argument to eventfreq(year,month). 
    The ct part of each pair is the number of records 
    that had a date equal to datetime.date(A,B,C). 

    One more requirement: sort the returned list 
    in increasing order by date (the sorted function will 
    do this for you) 

    Use fieldict("DOT1000.txt") to get the dictionary 
    of tuples used for building the list of pairs 
    that eventfreq(year,month) will return. 
    ''' 

這是我的努力:

def eventfreq(year, month): 
    N=fieldict('DOT1000.txt')[line][1] 
    L = [] # empty List for accumulation 
    for line in N: # data file is standard input 

       st = N[1] # get datetime.date 
       (L[st] = L.get(st,0) + 1) 
+1

我可警告你,現在這將被降低,試圖更具體地說明你正在努力解決的問題的哪一方面 –

+0

我清理了你的問題;但@ COpython是正確的,你需要問一個具體的問題。例如,_「我明白如何調用函數並循環遍歷結果,但我不知道如何去做____。我以這種方式嘗試了它,但是我得到了這個錯誤_____」 –

回答

0

嘗試檢查:

def eventfreq(year, month): 
    dates = [dt for x, dt, y in fieldict('DOT1000.txt') if dt.year==year and dt.month==month] 
    return [(dt, dates.count(dt)) for dt in set(dates)