Python 如何使用函数将新键和值附加到列表字典中

Python 如何使用函数将新键和值附加到列表字典中,python,list,function,dictionary,Python,List,Function,Dictionary,如果我有保存有关国家名称、人口和地区数据的dict1,并且我想使用函数以与当前词典相同的格式添加一个新的国家、人口和地区(其中值包含在列表中),我将如何做?请参阅addNew函数: {'Brazil': [19000000, 810000.00], 'Japan': [128000000, 350000.0]} dict1 = {} def appendDict(country, population, area): dict1[country] = [population, are

如果我有保存有关国家名称、人口和地区数据的
dict1
,并且我想使用函数以与当前词典相同的格式添加一个新的国家、人口和地区(其中值包含在列表中),我将如何做?

请参阅
addNew
函数:

{'Brazil': [19000000, 810000.00], 'Japan': [128000000, 350000.0]}
dict1 = {}

def appendDict(country, population, area):
    dict1[country] = [population, area]

appendDict("Brazil", "19000000", "810000.00")
appendDict("Japan", "128000000", "350000.0")

print(dict1)
输出:

dict1 = {'Brazil': [19000000, 810000.00], 'Japan': [128000000, 350000.0]}

def addNew(dict, country, population, area):
    dict.update({country: [population, area]})

addNew(dict1, 'countryTest', 200000, 1000.00)

print(dict1)

请参阅
addNew
功能:

dict1 = {}

def appendDict(country, population, area):
    dict1[country] = [population, area]

appendDict("Brazil", "19000000", "810000.00")
appendDict("Japan", "128000000", "350000.0")

print(dict1)
输出:

dict1 = {'Brazil': [19000000, 810000.00], 'Japan': [128000000, 350000.0]}

def addNew(dict, country, population, area):
    dict.update({country: [population, area]})

addNew(dict1, 'countryTest', 200000, 1000.00)

print(dict1)
使用以下代码:
dict1={‘巴西’:[19000000810000.00],‘日本’:[128000000350000.0]}#初始dict
def附加信息(c、p、a):
#在dict1中添加新值,这里我们添加国家(c)作为关键,[人口(p),面积(a)]作为值。
dict1[c]=[p,a]#dict[c]=[p,a]{‘巴西’:[19000000810000.0],‘日本’:[128000000350000.0],‘印度’:[100000000010098978.778]}
使用以下代码:
dict1={‘巴西’:[19000000810000.00],‘日本’:[128000000350000.0]}#初始dict
def附加信息(c、p、a):
#在dict1中添加新值,这里我们添加国家(c)作为关键,[人口(p),面积(a)]作为值。
dict1[c]=[p,a]#dict[c]=[p,a]{‘巴西’:[19000000810000.0],‘日本’:[128000000350000.0],‘印度’:[100000000010098978.778]}

以下是一个很好的例子:

dict1 = {'Brazil': [19000000, 810000.00], 'Japan': [128000000, 350000.0]} #initial dict

def addToDict(c, p, a):
    #add new values to dict1, here we are adding country(c) as key, [population(p), area(a)] as value.
    dict1[c] = [p, a] #dict[c] = [p,a] <-- [p,a] are values assigned to the key identified-in/new-key in dict1
    return dict1      #optional - I used it just to see the final value.

print (addToDict('India', 1000000000, 10098978.778)) #main call

#result --> {'Brazil': [19000000, 810000.0], 'Japan': [128000000, 350000.0], 'India': [1000000000, 10098978.778]}

下面是一个很好的例子:

dict1 = {'Brazil': [19000000, 810000.00], 'Japan': [128000000, 350000.0]} #initial dict

def addToDict(c, p, a):
    #add new values to dict1, here we are adding country(c) as key, [population(p), area(a)] as value.
    dict1[c] = [p, a] #dict[c] = [p,a] <-- [p,a] are values assigned to the key identified-in/new-key in dict1
    return dict1      #optional - I used it just to see the final value.

print (addToDict('India', 1000000000, 10098978.778)) #main call

#result --> {'Brazil': [19000000, 810000.0], 'Japan': [128000000, 350000.0], 'India': [1000000000, 10098978.778]}

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南,在这里申请。StackOverflow不是设计、编码、研究或教程资源。然而,若你们遵循你们在网上找到的任何资源,进行诚实的编码尝试,并遇到问题,你们将有一个很好的例子发布。欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南,在这里申请。StackOverflow不是设计、编码、研究或教程资源。然而,如果你遵循你在网上找到的任何资源,进行诚实的编码尝试,并遇到问题,你会有一个很好的例子来发布。为什么我们必须传递dict作为参数,如果说,它定义在函数上方?我只是认为它更一般。例如,如果您定义了dict2,那么您可以将它作为参数传递给函数并更新它。为什么我们还必须将dict作为参数传递,如果说它是在函数上方定义的呢?我只是认为它更一般。例如,如果定义
dict2
,则可以将其作为参数传递给函数并进行更新。虽然此代码可以回答问题,但提供有关如何和/或为什么解决问题的附加上下文将提高答案的长期价值。虽然此代码可以回答问题,提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。