Python 中的频率分析 - 打印带有频率的字母而不是带有频率的数字

发布于 2024-11-05 09:35:40 字数 312 浏览 6 评论 0原文

s=array1 #user inputs an array with text in it
n=len(s)
f=arange(0,26,1)
import collections
dict = collections.defaultdict(int)
for c in s:
    dict[c] += 1

for c in f:
    print  c,dict[c]/float(n)

在输出中, c 是数字而不是字母,我不知道如何将其转换回字母。

另外,有没有什么方法可以将频率/字母放入数组中,以便可以将它们绘制在直方图中?

s=array1 #user inputs an array with text in it
n=len(s)
f=arange(0,26,1)
import collections
dict = collections.defaultdict(int)
for c in s:
    dict[c] += 1

for c in f:
    print  c,dict[c]/float(n)

In the output, c is in numbers rather then letters and I'm not sure how to convert it back to letters.

Also, is there any way to get the frequency/letters into arrays so it'd be possible to plot them in a histogram?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痕至 2024-11-12 09:35:40

应该指出的是,您没有使用正确的类型调用 map参数(因此是 TypeError)。它需要一个函数和一个或多个应用该函数的可迭代对象。你的第二个参数是 toChar[i] ,它是一个字符串。所有可迭代对象都实现 __iter__。举例说明:

>>> l, t = [], ()
>>> l.__iter__
<<< <method-wrapper '__iter__' of list object at 0x7ebcd6ac>
>>> t.__iter__
<<< <method-wrapper '__iter__' of tuple object at 0x7ef6102c>

DTing 的答案让我想起了collections.Counter

>>> from collections import Counter
>>> a = 'asdfbasdfezadfweradf'
>>> dict((k, float(v)/len(a)) for k,v in Counter(a).most_common())
<<<
{'a': 0.2,
 'b': 0.05,
 'd': 0.2,
 'e': 0.1,
 'f': 0.2,
 'r': 0.05,
 's': 0.1,
 'w': 0.05,
 'z': 0.05}

It should be pointed out that you aren't calling map with the right type of arguments (thus the TypeError). It takes a single function and one or more iterables, to which the function is applied to. Your second argument is toChar[i] which would be a string. All iterables implement __iter__. To illustrate:

>>> l, t = [], ()
>>> l.__iter__
<<< <method-wrapper '__iter__' of list object at 0x7ebcd6ac>
>>> t.__iter__
<<< <method-wrapper '__iter__' of tuple object at 0x7ef6102c>

DTing's answer reminded me of collections.Counter:

>>> from collections import Counter
>>> a = 'asdfbasdfezadfweradf'
>>> dict((k, float(v)/len(a)) for k,v in Counter(a).most_common())
<<<
{'a': 0.2,
 'b': 0.05,
 'd': 0.2,
 'e': 0.1,
 'f': 0.2,
 'r': 0.05,
 's': 0.1,
 'w': 0.05,
 'z': 0.05}
最笨的告白 2024-11-12 09:35:40

如果您使用的是 python 2.7 或更高版本,您可以使用 collections.Counter

Python 2.7+

>>> import collections
>>> s = "I want to count frequencies."
>>> counter = collections.Counter(s)
>>> counter
Counter({' ': 4, 'e': 3, 'n': 3, 't': 3, 'c': 2, 'o': 2, 'u': 2, 'a': 1, 'f': 1, 'I': 1,     'q': 1, 'i': 1, 's': 1, 'r': 1, 'w': 1, '.': 1})
>>> n = sum(counter.values()) * 1.0   # Convert to float so division returns float.
>>> n
28
>>> [(char, count / n) for char, count in counter.most_common()]
[(' ', 0.14285714285714285), ('e', 0.10714285714285714), ('n', 0.10714285714285714), ('t', 0.10714285714285714), ('c', 0.07142857142857142), ('o', 0.07142857142857142), ('u', 0.07142857142857142), ('a', 0.03571428571428571), ('f', 0.03571428571428571), ('I', 0.03571428571428571), ('q', 0.03571428571428571), ('i', 0.03571428571428571), ('s', 0.03571428571428571), ('r', 0.03571428571428571), ('w', 0.03571428571428571), ('.', 0.03571428571428571)]

Python 3+

>>> import collections
>>> s = "I want to count frequencies."
>>> counter = collections.Counter(s)
>>> counter
Counter({' ': 4, 'e': 3, 'n': 3, 't': 3, 'c': 2, 'o': 2, 'u': 2, 'a': 1, 'f': 1, 'I': 1,     'q': 1, 'i': 1, 's': 1, 'r': 1, 'w': 1, '.': 1})
>>> n = sum(counter.values())
>>> n
28
>>> [(char, count / n) for char, count in counter.most_common()]
[(' ', 0.14285714285714285), ('e', 0.10714285714285714), ('n', 0.10714285714285714), ('t', 0.10714285714285714), ('c', 0.07142857142857142), ('o', 0.07142857142857142), ('u', 0.07142857142857142), ('a', 0.03571428571428571), ('f', 0.03571428571428571), ('I', 0.03571428571428571), ('q', 0.03571428571428571), ('i', 0.03571428571428571), ('s', 0.03571428571428571), ('r', 0.03571428571428571), ('w', 0.03571428571428571), ('.', 0.03571428571428571)]

这还将按频率降序返回 (char,Frequency) 元组。

If you are using python 2.7 or greater you can use collections.Counter.

Python 2.7+

>>> import collections
>>> s = "I want to count frequencies."
>>> counter = collections.Counter(s)
>>> counter
Counter({' ': 4, 'e': 3, 'n': 3, 't': 3, 'c': 2, 'o': 2, 'u': 2, 'a': 1, 'f': 1, 'I': 1,     'q': 1, 'i': 1, 's': 1, 'r': 1, 'w': 1, '.': 1})
>>> n = sum(counter.values()) * 1.0   # Convert to float so division returns float.
>>> n
28
>>> [(char, count / n) for char, count in counter.most_common()]
[(' ', 0.14285714285714285), ('e', 0.10714285714285714), ('n', 0.10714285714285714), ('t', 0.10714285714285714), ('c', 0.07142857142857142), ('o', 0.07142857142857142), ('u', 0.07142857142857142), ('a', 0.03571428571428571), ('f', 0.03571428571428571), ('I', 0.03571428571428571), ('q', 0.03571428571428571), ('i', 0.03571428571428571), ('s', 0.03571428571428571), ('r', 0.03571428571428571), ('w', 0.03571428571428571), ('.', 0.03571428571428571)]

Python 3+

>>> import collections
>>> s = "I want to count frequencies."
>>> counter = collections.Counter(s)
>>> counter
Counter({' ': 4, 'e': 3, 'n': 3, 't': 3, 'c': 2, 'o': 2, 'u': 2, 'a': 1, 'f': 1, 'I': 1,     'q': 1, 'i': 1, 's': 1, 'r': 1, 'w': 1, '.': 1})
>>> n = sum(counter.values())
>>> n
28
>>> [(char, count / n) for char, count in counter.most_common()]
[(' ', 0.14285714285714285), ('e', 0.10714285714285714), ('n', 0.10714285714285714), ('t', 0.10714285714285714), ('c', 0.07142857142857142), ('o', 0.07142857142857142), ('u', 0.07142857142857142), ('a', 0.03571428571428571), ('f', 0.03571428571428571), ('I', 0.03571428571428571), ('q', 0.03571428571428571), ('i', 0.03571428571428571), ('s', 0.03571428571428571), ('r', 0.03571428571428571), ('w', 0.03571428571428571), ('.', 0.03571428571428571)]

This will also return the (char, frequency) tuple in descending order of frequency.

§对你不离不弃 2024-11-12 09:35:40

要将数字转换为其代表的字母,只需使用内置的 chr

>>> chr(98)
'b'
>>> chr(66)
'B'
>>> 

To convert a number to the letter it represents, simply use the built-in chr:

>>> chr(98)
'b'
>>> chr(66)
'B'
>>> 
知你几分 2024-11-12 09:35:40
>>> a = "asdfbasdfezadfweradf"
>>> import collections
>>> counts = collections.defaultdict(int)
>>> for letter in a:
...     counts[letter]+=1
... 
>>> print counts
defaultdict(<type 'int'>, {'a': 4, 'b': 1, 'e': 2, 'd': 4, 'f': 4, 's': 2, 'r': 1, 'w': 1, 'z': 1})
>>> hist = dict( (k, float(v)/len(a)) for k,v in counts.iteritems())
>>> print hist
{'a': 0.2, 'b': 0.05, 'e': 0.1, 'd': 0.2, 'f': 0.2, 's': 0.1, 'r': 0.05, 'w': 0.05, 'z': 0.05}
>>> a = "asdfbasdfezadfweradf"
>>> import collections
>>> counts = collections.defaultdict(int)
>>> for letter in a:
...     counts[letter]+=1
... 
>>> print counts
defaultdict(<type 'int'>, {'a': 4, 'b': 1, 'e': 2, 'd': 4, 'f': 4, 's': 2, 'r': 1, 'w': 1, 'z': 1})
>>> hist = dict( (k, float(v)/len(a)) for k,v in counts.iteritems())
>>> print hist
{'a': 0.2, 'b': 0.05, 'e': 0.1, 'd': 0.2, 'f': 0.2, 's': 0.1, 'r': 0.05, 'w': 0.05, 'z': 0.05}
喜爱纠缠 2024-11-12 09:35:40

将频率/字母转换为数组:

hisArray = [dict[c]/float(n) for c in f]

To convert frequency/letters into arrays:

hisArray = [dict[c]/float(n) for c in f]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文