方法一:
a={'name':'Tom','age':20,'address':'北京','score':[89,99,78,95]}
sum = sum(a['score'])
ave = sum/4
print(ave)
方法二:
a={'name':'Tom','age':20,'address':'北京','score':[89,99,78,95]}
b = a['score']
print(b)
sum = 0
for i in b:
sum +=i
avg = sum/len(b)
print(avg)
keys |
返回一个包含字典所有key的列表 |
values |
返回一个包含字典所有vaule的列表 |
a={'001':{'name':'张三','age':20,'address':'北京','score':88},
'002':{'name':'李四','age':20,'address':'北京','score':78},
'003':{'name':'王五','age':20,'address':'北京','score':95},
'004':{'name':'小明','age':20,'address':'北京','score':90}
}
k = a['003'].keys()
print(k)
v = a['003'].values()
print(v)
a={'001':{'name':'张三','age':20,'address':'北京','score':88},
'002':{'name':'李四','age':20,'address':'山东','score':78},
'003':{'name':'王五','age':20,'address':'北京','score':95},
'004':{'name':'小明','age':20,'address':'河北','score':90}
}
for i in a.values():
if i['address'] =='北京':
i['住房补贴'] = 10000
print(a)
a={ '001':{'name':"李四",'age':28,'address':"山东",'score':88},
'002':{'name':"张三",'age':18,'address':"北京",'score':98},
'003':{'name':"小明",'age':48,'address':"河北",'score':90},
'004':{'name':"王五",'age':38,'address':"北京",'score':78}
}
max = a['001']['age']
min = a['001']['age']
for i in a.values():
if i['age']>max:
max = i['age']
elif i['age']
dic = {'k1': 'v1', 'k2': ['sb', 'aa'], (1, 2, 3, 4, 5): {'k3': ['2', 100, 'wer']}}
dic['k2'].append(33)
print(dic)
dic['k2'].insert(0,'s')
print(dic)
dic[(1,2,3,4,5)]['k4'] = 'v4'
dic[(1,2,3,4,5)][(1,2,3)] = 'ok'
dic[(1,2,3,4,5)]['k3'][2] = 'qq'