创建字母直方图 [英] Creating a Letter a Histogram

查看:123
本文介绍了创建字母直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想创建一个直方图. 这是我的代码:

So I want to create a histogram. Here is my code:

def histogram(s):
    d = dict()
    for c in s:
        if c not in d:
            d[c] = 1
        else:
            d[c] += 1
    return d

def print_hist(h):
    for c in h:
        print c, h[c]

它给我这个:

>>> h = histogram('parrot')
>>> print_hist(h)
a 1
p 1
r 2
t 1
o 1

但是我想要这个:

a: 1
o: 1
p: 1
r: 2
t: 1

那么我如何才能按字母顺序获取直方图,区分大小写(因此"a"和"A"相同),并列出整个字母(因此不在字符串中的字母只能为零) ?

So how can I get my histogram in alphabetical order, be case sensitive (so "a" and "A" are the same), and list the whole alphabet (so letters that are not in the string just get a zero)?

推荐答案

为此只需使用collections.Counter,除非您真的想要自己的:

Just use collections.Counter for this, unless you really want your own:

>>> import collections
>>> c = collections.Counter('parrot')
>>> sorted(c.items(), key=lambda c: c[0])
[('a', 1), ('o', 1), ('p', 1), ('r', 2), ('t', 1)]

编辑:正如评论员所指出的那样,您的最后一句话表示您希望获取单词中未出现的所有字母数据. Counter对此也有好处,因为文档表明:

EDIT: As commenters pointed out, your last sentence indicates you want data on all the letters of the alphabet that do not occur in your word. Counter is good for this also since, as the docs indicate:

Counter对象具有一个字典接口,只是它们为丢失的项目返回零计数而不是引发KeyError.

所以您可以迭代string.ascii_lowercase之类的东西:

So you can just iterate through something like string.ascii_lowercase:

>>> import string
>>> for letter in string.ascii_lowercase:
...   print('{}: {}'.format(letter, c[letter]))
... 
a: 1
b: 0
c: 0
d: 0
e: 0
f: 0
g: 0
h: 0
i: 0
j: 0
k: 0
l: 0
m: 0
n: 0
o: 1
p: 1
q: 0
r: 2
s: 0
t: 1
u: 0
v: 0
w: 0
x: 0
y: 0
z: 0

最后,不要执行复杂的操作以合并大小写字母的结果,而是先将输入标准化:

Finally, rather than implementing something complicated to merge the results of upper- and lowercase letters, just normalize your input first:

c = collections.Counter('PaRrOt'.lower())

这篇关于创建字母直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆