下面有很多代码向您展示我必须使用的技能水平才能完成此任务。初学者技巧只是请。
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,第6是C。
我需要单独执行以下每个月的操作,最后要有一个包含两个元素的元组; 0表示月份,1表示“average_price”,如下所示。我试图最终从原始列表中的每个列表中取出第5和第6个值,并按如下方式执行操作...(我需要使用BEGINNER TECHNIQUES for MY CLASS ...感谢您的理解)
average_price =(V1 * C1 + V2 * C2 + ... + Vn * Cn)/(V1 + V2 + ... + Vn)
(V =列表中的每个第5个元素C =列表中的每个第6个元素)
我的问题是只执行上述任务到一个月而不是整个列表,然后有一个结果,如,
[('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 :(得分:1)
这里的关键是停止使用列表并使用字典,它将为您整理事物。
通常你会使用集合模块中的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
我认为这解决了你在循环中遇到的大部分问题。
答案 1 :(得分: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),这不需要每月初始化。但也许这比你想要的要先进一点。