首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在访问python时获得以下错误

在访问python时获得以下错误
EN

Stack Overflow用户
提问于 2018-01-24 08:35:34
回答 1查看 46关注 0票数 0

在访问python时获得以下错误

代码语言:javascript
复制
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”对象不可订阅

EN

Stack Overflow用户

回答已采纳

发布于 2018-01-24 08:44:06

使用计数器比你试图实现的要容易得多:

代码语言:javascript
复制
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))

如果你想以“计数字母”的格式打印它们,那么:

代码语言:javascript
复制
res = ''
counts = Counter(alphas)
for count, letter in counts.items():
    res += '{}|{},'.format(count, letter)

print(res)
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48418020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档