作者lanpo (P)
看板Python
标题Re: [问题] 计算名字list中开头字母的人数
时间Tue Mar 17 22:37:15 2015
nameDict = {}
for name in nameList:
if name[0] not in nameDict:
nameDict[name[0]] = 1
else:
nameDict[name[0]] += 1
用dictionary来计算
字母当作key,出现次数当作value
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.36.28.48
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1426603038.A.F17.html
1F:推 SoldierGame: nameDict[name[0]] = nameDict.get(name[0], 0) + 1 03/18 00:45
2F:推 fischcheng: elegant solution! 03/18 04:29
3F:推 alair99: 漂亮~~ 03/20 17:04