码迷,mamicode.com
首页 > 其他好文 > 详细

练习:根据车牌信息,统计各省车牌持有量

时间:2018-08-05 20:30:42      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:car   local   信息   出现   print   列表   class   style   个数   

cars = ["鲁A32444","鲁B12333","京B8989M","黑C49555","沪B25041"]
locals = {"沪":"上海","黑":"黑龙江","鲁":"山东","京":"北京"}
结果:{"山东":2,"北京":1,"黑龙江":1,"上海":1}
cars = ["鲁A32444","鲁B12333","京B8989M","黑C49555","沪B25041"]
locals = {"":"上海","":"黑龙江","":"山东","":"北京"}

dic = {}
names = []
for i in cars:
    value = locals[i[0]] #黑龙江
    names.append(value) #[‘山东‘, ‘山东‘, ‘北京‘, ‘黑龙江‘, ‘上海‘]
set1 = set(names)  #{‘山东‘, ‘北京‘, ‘黑龙江‘, ‘上海‘}
for j in set1:
    dic.update({j:names.count(j)})  ##  注意 统计列表中不同元素出现的个数  元素作为key  出现的次数是value
print(dic)

 

练习:根据车牌信息,统计各省车牌持有量

标签:car   local   信息   出现   print   列表   class   style   个数   

原文地址:https://www.cnblogs.com/kenD/p/9426814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!