二楼的写法已经相当简洁了!再简洁的算法我是写不出来了,除非你想要复杂的,复杂的基本算法原理也差不多。
>>> s
'here is a sample of english test,test 1,test 3'
>>> d={}
>>> for c in s:
... if c in d:
... d[c]=d[c]+1
... else:
... d[c]=1
...
>>> print d
{'a': 2, ' ': 8, 'e': 7, 'g': 1, 'f': 1, 'i': 2, 'h': 2, 'm': 1, 'l': 2, 'o': 1,
'n': 1, '1': 1, 'p': 1, 's': 6, 'r': 1, 't': 6, ',': 2, '3': 1}
>>>
和二楼的基本一样,