python嵌套字典的用法
注意,嵌套中是list还是dict
例子:嵌套中是字典
d = { '春波':{ "car":{'BMW':5,'BENZ':3,'audi':3,'byd':4}, "house":["北京",'南京','上海'], "money":1000000,}, '美玲':{ "bag":['xx','lv',], "house":['三环','4环'], "hzp":['skii','ysl'], 'language':{ '普通话':10, '东北话':20, '日语':0, '英语':4 } } } #1、春波卖了一套上海的房子,卖了500w d['春波']['house'].pop(-1) d['春波']['money']+=5000000 print(d) #2、算一下春波有多少辆车 car_num=d['春波']['car'].values() print(sum(car_num)) #或者 sum=0 for n in car_num: sum=n+sum print(sum) #3、春波花了100w,买了一辆劳斯莱斯,卖掉之前的奥迪 d['春波']['car'].pop('audi') d['春波']['car']['LSLS']=3 d['春波']['money']-=1000000 print(d) #4、美玲的东北话退步了, 从20级变成了10级 d['美玲']['language']['东北话']=10 print(d) #5、美玲会哪些语言 language_s=d['美玲']['language'].keys() print(language_s)
嵌套中是list
d ={'春波':{'car':['BMW','ben-z','qq'],'house':['beijing','shanghai','nanjing','深圳'],'money':100000}, '乔美玲':{'bag':['lv','coach','pd'],'house':['ss','ssd','sddf'], 'language':{'普通话':10,'dongbaihua':20,'riyu':0,'yingyu':4}}} d['春波']['car'].append('laosilaisi') #增加一辆laosilaisi d['春波']['house'].pop(-1) #卖掉深圳的房子 d['春波']['house'].remove('nanjing') #卖掉 d['春波']['money'] = d['春波']['money'] + 5000000 #增加 print(d)