2012-12-13 14 views
0

下面有很多代碼向您展示我必須使用的技能級別才能完成此任務。初學者技術只請。如何對列表的不同列表中的某些索引執行操作,然後通過另一個索引將它們組合在一起

DEF get_monthly_averages(original_list):

#print(original_list) 
daily_averages_list = [ ] 
product_vol_close = [ ] # used for numerator 
monthly_averages_numerator_list = [ ] 
for i in range (0, len(original_list)): 
    month_list = original_list[i][0][0:7]  #Cutting day out of the date leaving Y-M 
    volume_str = float(original_list[i][5])  #V 
    adj_close_str = float(original_list[i][6])  #C 
    daily_averages_sublists = [month_list,volume_str,adj_close_str] #[Date,V,C] 
    daily_averages_list.append(daily_averages_sublists) 
for i in range (0, len(daily_averages_list)):  #Attempt at operation 
    vol_close = daily_averages_list[i][1]*daily_averages_list[i][2] 
    month_help = daily_averages_list[i][0] 
    product_vol_sublists = [month_help,vol_close] 
    product_vol_close.append(product_vol_sublists) 
    print(product_vol_close) 
    for i in range (0, len(product_vol_close)):  #<-------TROUBLE STARTS 
     for product_vol_close[i][0]==product_vol_close[i][0]: #When the month is the same 
      monthly_averages_numerator = product_vol_close[i][1]+product_vol_close[i][1] 
      # monthly_averages_numerator = sum(product_vol_close[i][1])   #tried both 
      month_assn = product_vol_close[i][0] 
      numerator_list_sublists = [month_assn,monthly_averages_numerator]     
      monthly_averages_numerator_list.append(numerator_list_sublists) 
      print(monthly_averages_numerator_list) 

原版是在形式:

[['2004-08-30', '105.28', '105.49', '102.01', '102.01', '2601000', '102.01'], 
['2004-08-27', '108.10', '108.62', '105.69', '106.15', '3109000', '106.15'], 
['2004-08-26', '104.95', '107.95', '104.66', '107.91', '3551000', '107.91'], 
['2004-08-25', '104.96', '108.00', '103.88', '106.00', '4598900', '106.00'], 
['2004-08-24', '111.24', '111.60', '103.57', '104.87', '7631300', '104.87'], 
['2004-08-23', '110.75', '113.48', '109.05', '109.40', '9137200', '109.40'], 
['2004-08-20', '101.01', '109.08', '100.50', '108.31', '11428600', '108.31'], 
['2004-08-19', '100.00', '104.06', '95.96', '100.34', '22351900', '100.34']] 

的0指數的時間,第5是V,第六是C.

我需要爲每個月單獨執行下面的操作,最後有一個包含兩個元素的元組; 0代表月份年份,1代表'average_price',如下所示。我試圖最終從原始列表中的每個列表中取出第5和第6個值,並執行如下操作...(我需要爲我的班級使用初級技術...感謝您的理解)

average_price =(V1 * C1 + V2 * C2 + ... + Vn * Cn)/(V1 + V2 + ... + Vn)

(V =列表中的每個第5個元素C =名單)

我的問題是,只有在執行上述任務,一個月單獨,而不是整個列表,然後有一個結果,例如,

[('month1',average_price),('month2',average_price),...] 

我做了

for i in range (0, len(product_vol_close)):  #<-------TROUBLE STARTS 
    for product_vol_close[i][0]==product_vol_close[i][0]: 

同月和今年他們組合在一起的時候。

嘗試展示我想要做的事情。我無法找到任何答案,如何讓我的工作方式,我想要的。

如果仍有混淆請評論!再次感謝您對此事的耐心,理解和幫助!

我完全失去了。

回答

0

我的建議是堅持數據行上的一個主循環。像這樣的東西(僞代碼):

current_month = None 
monthly_value = [] 
monthly_volume = [] 

for row in data: 
    date, volume, price = parse(row) # you need to write this yourself 
    month = month_from_date(date) # this too 

    if month != current_month: # do initialization for each new month 
     current_month = month 
     monthly_value.append(0) 
     monthly_volume.append(0) 

    monthly_value[-1] += volume*price # indexing with -1 gives last value 
    monthly_volume[-1] += volume 

然後你可以做第二個循環來計算平均值。請注意,這要求您的數據按月份分組。如果你的數據組織得不是很好,你可以用上面代碼中的列表替換字典(按月索引)。或者,您可以使用defaultdict(來自標準庫中的collections module),它不需要任何每月初始化。但也許這比你想要的更先進一點。

1

這裏的關鍵是停止使用列表並使用一個字典,它會照顧到爲你分組的東西。

通常情況下,您將使用collection模塊中的defaultdict,但由於這看起來像可能不被允許的作業,所以這裏是「長」的方法。

在您的示例數據中,每個日期只有一行,所以我將在代碼片段中假設相同。爲了讓我們的生活更輕鬆,我們將日期存儲在年份中;因爲這就是我們計算的基礎:

>>> date_scores = {} 
>>> for i in data: 
... year_month = i[0][:7] # this will be our key for the dictionary 
... if year_month not in date_scores: 
...   # In this loop, we check if the key exists or not; if it doesn't 
...   # we initialize the dictionary with an empty list, to which we will 
...   # add the data for each day. 
...   date_scores[year_month] = [] 
...  
... date_scores[year_month].append(i[1:]) # Add the data to the list for that 
...           # for the year-month combination 
... 
>>> date_scores 
{'2004-08': [['105.28', '105.49', '102.01', '102.01', '2601000', '102.01'], ['108.10', '108.62', '105.69', '106.15', '3109000', '106.15'], ['104.95', '107.95', '104.66', '107.91', '3551000', '107.91'], ['104.96', '108.00', '103.88', '106.00', '4598900', '106.00'], ['111.24', '111.60', '103.57', '104.87', '7631300', '104.87'], ['110.75', '113.48', '109.05', '109.40', '9137200', '109.40'], ['101.01', '109.08', '100.50', '108.31', '11428600', '108.31'], ['100.00', '104.06', '95.96', '100.34', '22351900', '100.34']]} 

現在對於每個年份組合,我們在字典中都有一個列表。該列表在每月的有子列表,我們有數據。現在我們可以做這樣的事情:

>>> print 'We have data for {} days for 2004-08'.format(len(date_scores['2004-08'])) 
We have data for 8 days for 2004-08 

我認爲這解決了大部分與循環有關的問題。

相關問題