繁体   English   中英

Python中的频率分析-使用频率而不是频率来打印字母

[英]Frequency Analysis in Python -Print letters with frequency rather than numbers with frequency

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是数字而不是字母,我不确定如何将其转换回字母。

另外,是否有任何方法可以将频率/字母分成数组,以便可以将它们绘制成直方图?

应该指出的是,您不是在使用正确类型的参数(因此是TypeError )来调用map 它需要一个函数和一个或多个迭代器,该函数将应用于该迭代器。 您的第二个参数是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}

如果您使用的是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)]

这也将以频率降序返回(字符,频率)元组。

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

>>> chr(98)
'b'
>>> chr(66)
'B'
>>> 
>>> 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}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM