Python 中計算字串中的字符出現次數


1
2
3
4
5
6
7
8
9
10
11
#縮成一行:
s = '[email protected]!{(]zyu&8jb'; print "\n".join(map(lambda x: "%s -> %d" % (x, s.count(x)), s))

#分成多行
s = '[email protected]!{(]zyu&8jb'
f = lambda x: "%s -> %d" % (x, s.count(x));
e = map(f, s)
print "\n".join(e)

#備用
print [i for i in s]