访问python时出现以下错误
from collections import Counter
alphas = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
res = ''
for char in alphas:
res = "{0},{1}|{2}".format(res , char, Counter[char])
print(res)
TypeError:' type'对象不可订阅
答案 0 :(得分:1)
使用计数器比你想要实现的要容易得多:
from collections import Counter
alphas = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
print(Counter(alphas))
如果你想以" count | letter"的格式打印它们。然后:
res = ''
counts = Counter(alphas)
for count, letter in counts.items():
res += '{}|{},'.format(count, letter)
print(res)