How to calculate the frequency of a list of symbols in Python -


i trying calculate frequency of amount of times each symbol shown in list. far have code doesn't seem work correctly because shows error message below...

uniques = set(words.split()) attributeerror: 'tuple' object has no attribute 'split' 

this code far...

def calculate_frequency():         words = ()         uniques = set(words.split())         freqs = [(item, words.split.count(item)) item in uniques]         print(freqs) 

the symbols want know frequency of , stored in variable 'words' shown below...

for frequency counts, use collections.counter(), , feed whole text; can remove newlines splitting , rejoining.

i'm assuming words global string newlines it; split on newlines str.splitlines():

from collections import counter  def calculate_frequency():     freqs = counter(''.join(words.splitlines()))     symbol, count in freqs.most_common():         print symbol, count 

this produces frequency list sorted common least common symbol.

demo:

>>> 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())) ...     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 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -