我是Python和编程的新手。
我的教科书说我必须做以下问题集:
创建第二个采购摘要,该摘要通过股票代码符号累计总投资。在里面 在上面的样本数据中,有两个CAT块。
这些可以通过创建一个字典来轻松组合 关键是股票代码,价值是购买的块列表。该程序一次通过 通过数据创建字典。然后,通过dict可以创建显示每个dict的报告 股票代码和所有股票。
除了硬编码之外,我想不出添加'CAT'股票的两个条目的方法。
## Stock Reports
stockDict = {"GM":"General Motors", "CAT":"Caterpillar", "EK":"Eastman Kodak",
"FB":"Facebook"}
# symbol,prices,dates,shares
purchases = [("GM",100,"10-sep-2001",48), ("CAT",100,"01-apr-1999",24),
("FB",200,"01-jul-2013",56), ("CAT", 200,"02-may-1999",53)]
# purchase history:
print "Company", "\t\tPrice", "\tDate\n"
for stock in purchases:
price = stock[1] * stock[3]
name = stockDict[stock[0]]
print name, "\t\t", price, "\t", stock[2]
print "\n"
# THIS IS THE PROBLEM SET I NEED HELP WITH:
# accumulate total investment by ticker symbol
byTicker = {}
# create dict
for stock in purchases:
ticker = stock[0]
block = [stock]
if ticker in byTicker:
byTicker[ticker] += block
else:
byTicker[ticker] = block
for i in byTicker.values():
shares = i[0][3]
price = i[0][1]
investment = shares * price
print investment
现在,输出是:
4800
11200
2400
这不好,因为它不计算两只CAT股票。现在它只计算一个。代码应该足够灵活,以便我可以添加更多的CAT股票。
答案 0 :(得分:0)
也许是这样的:
## Stock Reports
stockDict = {"GM":"General Motors", "CAT":"Caterpillar", "EK":"Eastman Kodak",
"FB":"Facebook"}
# symbol,prices,dates,shares
purchases = [("GM",100,"10-sep-2001",48), ("CAT",100,"01-apr-1999",24),
("FB",200,"01-jul-2013",56), ("CAT", 200,"02-may-1999",53)]
# purchase history:
print "Company", "\t\tPrice", "\tDate\n"
for stock in purchases:
price = stock[1] * stock[3]
name = stockDict[stock[0]]
print name, "\t\t", price, "\t", stock[2]
print "\n"
# THIS IS THE PROBLEM SET I NEED HELP WITH:
# accumulate total investment by ticker symbol
byTicker = {}
# create dict
for stock in purchases:
ticker = stock[0]
price = stock[1] * stock[3]
if ticker in byTicker:
byTicker[ticker] += price
else:
byTicker[ticker] = price
for ticker, price in byTicker.iteritems():
print ticker, price
我得到的输出是:
Company Price Date
General Motors 4800 10-sep-2001
Caterpillar 2400 01-apr-1999
Facebook 11200 01-jul-2013
Caterpillar 10600 02-may-1999
GM 4800
FB 11200
CAT 13000
似乎是正确的。
测试byTicker
字典中是否有自动收报机,告诉您是否已经记录了该股票的购买情况。如果有,你只需添加它,如果没有,你开始新鲜。这基本上就是你正在做的事情,除非你出于某种原因收集该字典中给定股票的所有购买记录,而你真正关心的只是购买价格。
您可以按照原来的方式构建dict,然后迭代存储在每个键下的项目,然后将它们添加起来。像这样:
totals = []
for ticker in byTicker:
total = 0
for purchase in byTicker[ticker]:
total += purchase[1] * purchase[3]
totals.append((ticker, total))
for ticker, total in totals:
print ticker, total
只是为了踢,你可以用生成器语句将它全部压缩成一行:
print "\n".join("%s: %d" % (ticker, sum(purchase[1]*purchase[3] for purchase in byTicker[ticker])) for ticker in byTicker)
最后两个中的任何一个都是完全没必要的,因为你已经在每次购买时都在迭代,你也可以随时积累每个股票的总价格,正如我在第一个例子中所示。
答案 1 :(得分:0)
您的问题出现在代码的最后一部分,倒数第二位会针对每个股票代码创建所有股票的清单,这很好:
for i in byTicker.values():
shares = i[0][3]
price = i[0][1]
investment = shares * price
print investment
此处仅使用每个股票代码的第0个股票。相反,尝试:
for name, purchases in byTicker.items():
investment = sum(shares * price for _, shares, _, price in purchases)
print name, investment
这将为每个股票代码添加所有股票,并且您的示例为我提供:
CAT 13000
FB 11200
GM 4800
答案 2 :(得分:0)
您的代码存在的问题是您没有迭代购买者,只是从每个股票代码值中获取第一个元素。也就是说,byTicker看起来像:
byTicker: {
"GM": [("GM",100,"10-sep-2001",48)],
"CAT": [("CAT",100,"01-apr-1999",24), ("CAT", 200,"02-may-1999",53)],
"FB": [("FB",200,"01-jul-2013",56)]
}
所以当你迭代这些值时,你实际上得到了三个列表。但是当你处理这些列表时,你实际上只访问了第一个列表:
price = i[0][1]
对应于“CAT”的值,i [0]是(“CAT”,100,“01-apr-1999”,24)。你也应该看看我[1]!考虑迭代不同的购买:
for company, purchases in byTicker.items():
investment = 0
for purchase in purchases:
investment += purchase[1] * purchase[3]
print(company, investment)