Re: [问题] 计算名字list中开头字母的人数

楼主: lanpo (HCJP)   2015-03-17 22:37:15
nameDict = {}
for name in nameList:
if name[0] not in nameDict:
nameDict[name[0]] = 1
else:
nameDict[name[0]] += 1
用dictionary来计算
字母当作key,出现次数当作value
作者: SoldierGame (池面三人组)   2015-03-18 00:45:00
nameDict[name[0]] = nameDict.get(name[0], 0) + 1
作者: fischcheng (布阿送)   2015-03-18 04:29:00
elegant solution!
作者: alair99 (I think home)   2015-03-20 17:04:00
漂亮~~

Links booklink

Contact Us: admin [ a t ] ucptt.com