《Think Python》笔记2

统计字符串中各个字符的个数

def histpgram(s):
    d=dict()
    for c in s:
        if c not in d:
            d[c]=1
        else:
            d[c]+=1
    return d

string='jdkfjkfhdlkfhgkhklfdhfheifhedskjhflksdhdhfdhfdshfhdhfs'
print histpgram(string)


 

你可能感兴趣的:(《Think Python》笔记2)