我正在尝试计算每个符号在列表中显示的次数的频率。到目前为止,我有这个代码似乎无法正常工作,因为它显示下面的错误信息...
uniques = set(words.split())
AttributeError: 'tuple' object has no attribute 'split'
到目前为止,这是我的代码......
def calculate_frequency():
words = ()
uniques = set(words.split())
freqs = [(item, words.split.count(item)) for item in uniques]
print(freqs)
我想知道变量'words'的频率和存储的符号如下所示......
答案 0 :(得分:4)
对于频率统计,请使用collections.Counter()
,然后将其全文输入;我们可以通过拆分和重新加入来删除换行符。
我假设words
是一个带换行符的全局字符串;使用str.splitlines()
分隔新行:
from collections import Counter
def calculate_frequency():
freqs = Counter(''.join(words.splitlines()))
for symbol, count in freqs.most_common():
print symbol, count
这会产生一个从最常见的符号到最不常见的符号排序的频率列表。
演示:
>>> from collections import Counter
>>> words = '''\
... #+/084&"
... #3*#%#+
... 8%203:
... ,1$&
... !-*%
... .#7&33&
... #*#71%
... &-&641'2
... #))85
... 9&330*
... '''
>>> def calculate_frequency():
... freqs = Counter(''.join(words.splitlines()))
... for symbol, count in freqs.most_common():
... print symbol, count
...
>>> calculate_frequency()
# 8
& 7
3 6
% 4
* 4
1 3
0 3
8 3
) 2
+ 2
- 2
2 2
4 2
7 2
! 1
" 1
$ 1
' 1
, 1
/ 1
. 1
5 1
6 1
9 1
: 1
答案 1 :(得分:2)
您可以使用Counter()
查找每个角色的频率。
from collections import Counter
a="@#@#$%$#^@&"
print Counter(a)
#output Counter({'@': 3, '#': 3, '$': 2, '%': 1, '&': 1, '^': 1})
如果您有一个字符串列表,例如a = ["#@#$$","@#$@$$","@#!@#!@"]
,那么您可以使用Counter(''.join(a))
。