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

统计名字列表中,各名字的首字母在名字列表中出现的次数

时间:2018-02-24 22:00:54      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:统计名字列表中各名字的首字母在名字列表

name_list=[‘foster‘,"janet",‘jessus‘,‘david‘] count_dict={} for i in name_list: count_dict[i]="".join(name_list).count(i[0]) print count_dict
>>> def countfist(a):
...     res = {}
...     for i in a:
...         res[i] = "".join(a).count(i[0])
...     return res
...
>>> countfist(new_mystr)
{‘and‘: 3, ‘world‘: 1, ‘itcastcpp‘: 2, ‘itcast‘: 2, ‘hello‘: 1}
>>>
>>> name_list=[‘foster‘,"janet",‘jessus‘,‘david‘]
>>> countfist(name_list)
{‘david‘: 2, ‘foster‘: 1, ‘janet‘: 2, ‘jessus‘: 2}
>>>

统计名字列表中,各名字的首字母在名字列表中出现的次数

标签:统计名字列表中各名字的首字母在名字列表

原文地址:http://blog.51cto.com/357712148/2072736

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