在Python中迭代一个嵌套字典 [英] Iterate a Nested Dictionary in Python

查看:135
本文介绍了在Python中迭代一个嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字典结构如此:

stockData = {
    'AAPL': {
        'beta': 1.01833975315094,
        'company_name': 'Apple',
        'dividend': 1.9341673320912078, 
        'total':300}, 
    'GOOG': {
        'beta': 1.01833975315094,
        'company_name': 'Apple',
        'dividend': 1.9341673320912078, 
        'total':300}
     }

这里是我分配值的区域:

and here is area where i assign values:

for row in range(2, sheet.max_row + 1):
    # Each row in the spreadsheet has data for one census tract.
    company_name  = sheet['A' + str(row)].value
    ticker = sheet['B' + str(row)].value
    sector = sheet['C' + str(row)].value
    shares = sheet['D' + str(row)].value
    price = sheet['E' + str(row)].value
    total = sheet['F' + str(row)].value
    beta = sheet['G' + str(row)].value
    dividend = sheet['H' + str(row)].value
    number_stocks+=1

    # Make sure the key for this ticker exists.
    stockData.setdefault(ticker,{})
    stockData[ticker]['company_name'] = company_name
    stockData[ticker]['sector'] = sector
    stockData[ticker]['shares'] = shares
    stockData[ticker]['price'] = price
    stockData[ticker]['total'] = total
    stockData[ticker]['dividend'] = dividend
    stockData[ticker]['beta'] = beta

我有一个问题,我正在迭代使用一个for循环在字典上添加一个新的值'percentage' 总除以所有股票的总和的总和。

I'm having an issue where i am iterating using a for loop over the dictionary to add a new value 'percentage' which is the 'total' divided by the sum of all the totals of all stocks.

def get_portfolio_value(stocks,item):
    portValue = 0
    percentage = 0
    for k, v in stocks.items():
        portValue = portValue + v.get(item,0)
        stockData[ticker]['percentage'] = stockData[ticker]['total']/portValue
    print(portValue)

get_portfolio_value(stockData,'total')

问题是get_portfolio_value函数使我获得了整个portValue的投资组合,但是我试图将投资组合的百分比添加到每个股票的行是不行的 - 只有只有一个股票奇怪地出现。有人可以建议吗?

the issue is that the get_portfolio_value function is getting me the entire portValue total of the portfolio, but the line where i am trying to add the percentage of the portfolio to each stock isn't working righ--it is only oddly enough appearing for only one of the stocks. Can someone advise?

推荐答案

如果我明白你的问题正确,你可以尝试如下(我刚刚复制了 stockData 以下):

If I understand you question correctly, you may try as follows (I just copied the stockData below):

stockData = {
    'AAPL': {
        'beta': 1.01833975315094,
        'company_name': 'Apple',
        'dividend': 1.9341673320912078, 'total': 300},
    'GOOG': {
        'beta': 1.01833975315094,
        'company_name': 'Apple',
        'dividend': 1.9341673320912078, 'total': 300}}

我猜这是你的意思:

def get_portfolio_value(stocks, item='total'):
     portValue = 0
     for v in stocks.values():
        portValue += v[item]
     for k in stocks.keys():
        stocks[k].update({'percentage': stocks[k]['total'] / portValue})
    print(portValue)

get_portfolio_value(stockData, 'total')

这篇关于在Python中迭代一个嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆