访问python时出现以下错误

时间:2018-01-24 08:35:35

标签: python-3.x

访问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'对象不可订阅

1 个答案:

答案 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)