共計 212 個字符,預計需要花費 1 分鐘才能閱讀完成。
可以使用 collections 庫中的 Counter 類來統計字符串中字符的出現次數。示例如下:
from collections import Counter
s = "hello world"
counts = Counter(s)
for char, count in counts.items():
print(f"{char}: {count}")
運行以上代碼,輸出結果為:
h: 1
e: 1
l: 3
o: 2
: 1
w: 1
r: 1
d: 1
丸趣 TV 網 – 提供最優質的資源集合!
正文完