对新字典使用字典理解

时间:2020-09-18 01:46:48

标签: python dictionary dictionary-comprehension

被告知此问题已得到解答-apoligies 谢谢!

3 个答案:

答案 0 :(得分:0)

由于您想要一个函数来执行此操作:

def newprice(d):
  return {"all" + key: value * 5 for key, value in d.items()}

# To be used as :

dict1 = {'apple':5, 'banana':4}
print(newprice(dict1))

答案 1 :(得分:0)

在字典理解中,您可以对键和值做任何想做的事情:

def newprice(d):
  return {f'all{k}': v * 5 for k, v in d.items()}

答案 2 :(得分:0)

您在这里-

dict1 = {'apple':5, 'banana':4} 
dict2 = {('all'+ k): 5*dict1[k] for k in dict1 }
print(dict2)