Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在不使用 if、while 或 for 的情况下计算字符串中的字符(字符频率)?
您可以使用 Counter,它返回一个以字符为键、频率为值的字典。
from collections import Counter x = "abcdasbdd" print(Counter(x))
输出
Counter({'d': 3, 'a': 2, 'b': 2, 'c': 1, 's': 1})