gpt4 book ai didi

python - 类中的嵌套字典

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:59 24 4
gpt4 key购买 nike

<分区>

我遇到此数据结构的意外行为。

    class IncomeVerification(object):

data = {'IncomeYears': []}

def __init__(self, income_years):
for year in income_years:
new_year = IncomeYear(year).data
self.data['IncomeYears'].append(new_year)

class IncomeYear(object):
data = {'IncomeYear': {'Year': None}}

def __init__(self, year, reported_income=None):
self.data['IncomeYear']['Year'] = year

income_years = ['2014', '2013', '2012', '2011']

foo = IncomeVerification(income_years)
print foo.data

返回,

     {'IncomeYears': [{'IncomeYear': {'Year': '2011'}}, {'IncomeYear': {'Year': '2011'}}, {'IncomeYear': {'Year': '2011'}}, {'IncomeYear': {'Year': '2011'}}]}

我很期待,

    {'IncomeYears': [{'IncomeYear': {'Year': '2014'}}, {'IncomeYear': {'Year': '2013'}}, {'IncomeYear': {'Year': '2012'}}, {'IncomeYear': {'Year': '2011'}}]}

我相信我的词典的嵌套性质会产生独特的关键问题,因此会覆盖现有条目。关于如何修改上述调用以获得我想要的结果的任何想法?

谢谢。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com