简体   繁体   English

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

[英]How to append a new key and value into a dictionary of lists using a function

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

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

Use below code:使用以下代码:

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]}

See the addNew function:查看addNew函数:

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)

Output:输出:

{'Brazil': [19000000, 810000.0], 'Japan': [128000000, 350000.0], 'countryTest': [200000, 1000.0]}
pop = 10
area = 20
name = "A"
dict1[name] = [pop, area]
dict1 = {}

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

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

print(dict1)

Here is a good example:这是一个很好的例子:

travel_log = [
{
  "country": "France",
  "visits": 12,
  "cities": ["Paris", "Lille", "Dijon"]
},
{
  "country": "Germany",
  "visits": 5,
  "cities": ["Berlin", "Hamburg", "Stuttgart"]
},
]

dict1 = {}
def add_new_country(country,visits,cities):
  dict1 = {}
  dict1['country']= country
  dict1['visits'] = visits
  dict1['cities'] = cities
  travel_log.append(dict1)

add_new_country("Russia", 2, ["Moscow", "Saint Petersburg"])
print(travel_log)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 append 键和值(列表)添加到新字典? - How to append key and value (list) to new dictionary? python列表字典追加到新关键字 - python dictionary of lists append to new key 如何将新键和值附加到 yaml 字典文件 - How to append new key and value to a yaml dictionary file 如何在Python中将新的一组键值对添加到字典中? - How to append a new set of key,value pair to a dictionary in Python? 如何将新键添加到现有字典并将前一个键作为值附加到 for 循环中创建的新键:python - How to add a new key to an existing dictionary and append previous key as value to the new key created in a for loop : python 当键是字符串的第二个值时,如何将多个字符串列表附加到字典中的键? - How can I append multiple lists of strings to a key in dictionary when the key is the second value of a string? 如何在另一个字典中将字典附加为键的值? - How to append a dictionary as a value of a key in another dictionary? 如何使用python将多个键和值附加到嵌套字典中? - How to append multiple key and value into a nested dictionary using python? 如何将字典列表附加到字典中的键值? - How to append a list of dictionaries to the value of a key in a dictionary? 如何在字典中附加键值? - How to append key-value in dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM