【发布时间】:2019-01-30 23:08:31
【问题描述】:
我想编写一个函数,将字典作为输入并返回一个新字典。在新字典中,我想使用与旧字典相同的键,但我有新值。
这是我的旧词典:
animals = {'tiger': ['claws', 'sharp teeth', 'four legs', 'stripes'],
'elephant': ['trunk', 'four legs', 'big ears', 'gray skin'],
'human': ['two legs', 'funny looking ears', 'a sense of humor']
}
然后我正在创建一个接收旧字典的函数,我希望它保留键但更改值(新值应该通过一个名为 bandit 的函数。它看起来像这样。
def myfunction(animals):
new_dictionary = {}
for key in animals.keys():
new_dictionary = {key: []}
for value in animals[key]:
bandit_animals = bandit_language(value)
new_dictionary = {key: bandit_animals}
return new_dictionary
print(myfunction(animals))
该函数只打印最后一个键和最后一个值,我希望它打印整个字典。
谁能解释一下?
【问题讨论】:
标签: python python-3.x