3-4 字典的嵌套
1、字典的嵌套
1 stu_info = { 2 'xiaoming':{"password":123456,'phone':132323,'age':18,'addr':'北京'}, 3 'xiaohei':{'password':123456,'phone':1223423,'age':18,'addr':'上海'} 4 }
2、嵌套字典的取值
1 stu_info = { 2 'xiaoming':{"password":123456,'phone':132323,'age':18,'addr':'北京'}, 3 'xiaohei':{'password':123456,'phone':1223423,'age':18,'addr':'上海'} 4 } 5 print(stu_info['xiaoming']['phone'])
3、嵌套字典的增删改查--案例
1 d = { 2 '春波':{ 3 "car":{'BMW':5,'BENZ':3,'audi':3,'byd':4}, 4 "house":["北京",'南京','上海'], 5 "money":1000000,}, 6 '乔美玲':{ 7 "bag":['xx','lv',], 8 "house":['三环','4环'], 9 "hzp":['skii','ysl'], 10 'language':{ 11 '普通话':10, 12 '东北话':20, 13 '日语':0, 14 '英语':4 15 } 16 } 17 } 18 car_nums = d['春波']['car'].values() 19 20 21 # 1、春波卖了一套上海的房子,卖了500w 22 d['春波']['house'].pop(-1) 23 d['春波']['house'].remove('上海') 24 d['春波']['money']=d['春波']['money']+5000000 25 d['春波']['money']+=5000000 26 27 28 # 3、春波花了100w,买了一辆劳斯莱斯 29 d['春波']['money']=d['春波']['money']-10000 30 d['春波']['car'].append('劳斯莱斯') 31 32 # 4、乔美玲的东北话退步了, 从20级变成了10级 33 d['乔美玲']['language']['东北话']=10 34 35 # 5、乔美玲会哪些语言 36 print(d['乔美玲']['language'].keys()) 37 38 # 2、算一下马春波有多少辆车 39 print(car_nums) 40 print(sum(car_nums)) 41 sum = 0 42 for n in car_nums: 43 sum = n + sum 44 print(sum) 45 print(d)