- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上我有一个包含数据和价格的元组列表,例如:
[ ("2013-02-12", 200.0), ("2012-02-25", 300.0), ("2000-03-04", 100.0), ("2000-03-05", 50.0)]
该函数需要找到每个月的平均股票值(value),然后返回包含日期(月份和年份)和股票价格的元组列表。像这样的东西:
[(250.0, "02-2013"), (100.0, "03-2000"), (50.0, "03-2000")]
这是我目前的代码:
def average_data(list_of_tuples = []):
list_of_averages = []
current_year_int = 2013
current_month_int = 2
sum_float = float()
count = 0
for dd_tuple in list_of_tuples:
date_str = dd_tuple[0]
data_float = dd_tuple[1]
date_list = date_str.split("-")
year_int = int(date_list[0])
month_int = int(date_list[1])
date_year_str = "Date: " + str(month_int) + "-" + str(year_int);
if month_int != current_month_int:
average_float = sum_float / count
average_list = [date_year_str, average_float]
average_tuple = tuple(average_list)
list_of_averages.append(average_tuple)
current_month_int = month_int
sum_float += data_float
sum_float += data_float
count += 1
current_month_int = month_int
current_year_int = year_int
return list_of_averages
它返回一个平均值,但不是正确的,也许不是全部?我曾尝试查看互联网上的示例并询问我的助教(这是针对 python 类的)但无济于事。有人能指出我正确的方向吗?
编辑:根据建议,if 语句现在应该如下所示,对吗?
if month_int != current_month_int:
average_float = sum_float / count
average_list = [date_year_str, average_float]
average_tuple = tuple(average_list)
list_of_averages.append(average_tuple)
current_month_int = month_int
sum_float = 0.0
count = 0
sum_float += data_float
count += 1
编辑:感谢大家的帮助!我现在已经运行了代码。
最佳答案
>>> lis = [ ("2013-02-12", 200.0), ("2012-02-25", 300.0), ("2000-03-04", 100.0), ("2000-03-05", 50.0)]
>>> from collections import defaultdict
>>> dic = defaultdict(list)
>>> for k,val in lis:
key = "-".join(k.split('-')[:-1][::-1])
dic[key].append(val)
...
>>> [(sum(v)/float(len(v)),k) for k,v in dic.items()]
[(200.0, '02-2013'), (300.0, '02-2012'), (75.0, '03-2000')]
上述代码的简化版本:
lis = [ ("2013-02-12", 200.0), ("2012-02-25", 300.0), ("2000-03-04", 100.0), ("2000-03-05", 50.0)]
dic = {}
for date, val in lis:
#split the date string at '-' and assign the first 2 items to year,month
year, month = date.split('-')[:2]
#now check if (month,year) is there in the dict
if (month, year) not in dic:
#if the tuple was not found then initialise one with an empty list
dic[month,year] = []
dic[month,year].append(val) # append val to the (month,year) key
print dic
#Now iterate over key,value items and do some calculations to get the desired output
sol =[]
for key, val in dic.items():
new_key = "-".join(key)
avg = sum(val) / len(val)
sol.append((avg, new_key))
print sol
输出:
#print dic
{('03', '2000'): [100.0, 50.0],
('02', '2013'): [200.0],
('02', '2012'): [300.0]}
#print sol
[(75.0, '03-2000'), (200.0, '02-2013'), (300.0, '02-2012')]
关于Python:查找每个月的平均股票值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17016731/
我正在处理现有网站的移动版本,我无法解决菜单中链接的问题。 该问题仅发生在标准的 android 浏览器上。在 Chrome、firefox、safari 甚至 IE 上,该网站都运行良好。该网站上的
几周来我一直在努力解决这个问题,但没有找到真正的解决方案。我发现了一种解决方法,但我觉得它很烦人。 图像在我的 Galaxy S3 的默认浏览器中加载模糊,但在 chrome 和 firefox 中它
安装了多个浏览器。我怎样才能打开http://www.google.com以编程方式使用内置(库存)浏览器? 最佳答案 使用内置浏览器,通常可以通过按菜单按钮使地址栏出现(当然是在按图标打开浏览器之后
我在面试中被问到这样的问题: 给定股票价格: MS | 500 Apl | 1000 Nefx| 500 MS | 500 每次新库存到来时,我们都必须添加到现有库存中,否则如果是新
我需要将每个键的值相乘,然后将所有值相加以打印一个数字。我知道这可能非常简单,但我被卡住了 在我看来,我会用类似的方式来解决这个问题: for v in prices: total = sum(v *
直到昨天这样的查询 http://autoc.finance.yahoo.com/autoc?query=a&callback=YAHOO.Finance.SymbolSuggest.ssCallba
我正在尝试找到一个在phonegap应用程序中绘制折线/股票图表的解决方案。我尝试过很多库:amcharts JS、highcharts,但没有一个能工作。 有人可以帮我完成这个任务吗?欢迎任何解决方
如果您在 Google 上查看股票(例如 search for 'Apple stocks' ),您会得到一个相当漂亮且交互式的图表,如下所示: 请注意垂直十字线和漂亮的工具提示。 事实证明,尝试在
首先,我必须说,我是人工智能方面的初学者。我遵循了大多数有关股市预测的教程,它们几乎都是相同的。这些教程使用一个数据集并分为两组。第一个是训练集,第二个是测试集。他们正在使用股票的收盘价来训练和制作模
最近在使用highchart stock(highstock.js)的时候遇到了一个很奇怪的问题。我加载了一些包含星期六数据点的数据点。当应用程序运行时,起初它看起来像这样: 没有图表出现,只有导航器
我已经在 Azure 中的存储帐户上部署了新的文件共享,自从我这样做以来,我不再能够执行 terraform 计划,而是收到以下错误: azurerm_storage_account_customer
我是一名优秀的程序员,十分优秀!