Get the number of occurrences of each character

后端 未结 7 1533
说谎
说谎 2020-12-09 23:05

Given the string:

a=\'dqdwqfwqfggqwq\'

How do I get the number of occurrences of each character?

相关标签:
7条回答
  • 2020-12-09 23:48

    In 2.7 and 3.1 there's a tool called Counter:

    >>> import collections
    >>> results = collections.Counter("dqdwqfwqfggqwq")
    >>> results
    Counter({'q': 5, 'w': 3, 'g': 2, 'd': 2, 'f': 2})
    

    Docs. As pointed out in the comments it is not compatible with 2.6 or lower, but it's backported.

    0 讨论(0)
提交回复
热议问题