14.有如下车牌和车辆归属地,形成一个新的字典,显示每个归属地的车辆共有多少:
作者:互联网
有如下车牌和车辆归属地,形成一个新的字典,显示每个归属地的车辆共有多少:
cars = ['鲁A32444','鲁B12333','京B8989M','⿊C49678','⿊C46555','沪B25044','冀G11111','⿊C42355']
locals = {'冀':'河北', '⿊':'⿊⻰江', '鲁':'⼭东', '鄂':'湖北', '湘':'湖南','京':'北京'}
结果: {'⿊⻰江':3, '⼭东': 2, '北京': 1,'河北':1}
第一种:
dic={}
li=[]
for i in cars:
if i[0] in locals:
li.append(i)
for j in li:
dic[locals[j[0]]]=dic.get(locals[j[0]],0)+1
print(dic)
第二种:
dic = {}
for i in cars:
if locals.get(i[0]):
dic[locals[i[0]]] = dic.get(locals[i[0]],0) + 1
print(dic)
标签:14,get,cars,归属,li,车辆,dic,locals 来源: https://www.cnblogs.com/pythonblogs/p/11025326.html