大家晚上好哇,好久不见(其实也就两天嘻嘻)
回归正题,今日分享实例之升职加薪,也就是字典内容的应用,比如字典的嵌套使用,其中还包括for循环的巩固使用。
如图所示:
源代码如下:
#定义一个字典 dict1={'王力宏':{'department':'科技','money':3000,'grade':1}, '周杰伦':{'department':'市场','money':5000,'grade':2}, '林俊杰':{'department':'市场','money':7000,'grade':3}, '张学友':{'department':'科技','money':4000,'grade':1}, '刘德华':{'department':'市场','money':6000,'grade':2}} print(dict1) #for循环,遍历dict1 for name in dict1: if dict1[name]['grade']==1: #将dict1中的value值赋值给dict2 dict2=dict1[name] # print(dict2) dict2['grade']+=1 dict2['money']+=1000 # print(dict2) #再将赋给dict2的值重新赋回dict1 dict1[name]=dict2 print(dict1)
不早咯,晚安家人们,希望有个愉快的周末~