-1

如何在不使用 if、while 或 for 的情况下计算字符串中的字符(字符频率)?

4

1 回答 1

0

您可以使用 Counter,它返回一个以字符为键、频率为值的字典。

from collections import Counter
x = "abcdasbdd"
print(Counter(x))

输出

Counter({'d': 3, 'a': 2, 'b': 2, 'c': 1, 's': 1})
于 2021-03-06T16:28:12.883 回答