Calculating the Letter Frequency in Python -
i need define function slice string according character, sum indices, divide number of times character occurs in string , divide length of text.
here's have far:
def ave_index(char): passage = "string" if char in passage: word = passage.split(char) words = len(word) number = passage.count(char) answer = word / number / len(passage) return(answer) elif char not in passage: return false
so far, answers i've gotten when running have been quite off mark
edit: passage given use string - 'call me ishmael. years ago - never mind how long precisely - having little or no money in purse, , nothing particular interest me on shore, thought sail little , see watery part of world. way have of driving off spleen , regulating circulation. whenever find myself growing grim mouth; whenever damp, drizzly november in soul; whenever find myself involuntarily pausing before coffin warehouses, , bringing rear of every funeral meet; , whenever hypos such upper hand of me, requires strong moral principle prevent me deliberately stepping street, , methodically knocking people's hats off - then, account high time sea can. substitute pistol , ball. philosophical flourish cato throws himself upon sword; quietly take ship. there nothing surprising in this. if knew it, men in degree, time or other, cherish same feelings towards ocean me.'
when char = 's' answer should 0.5809489252885479
the problem how counting letters. take string hello world
, trying count how many l
there are. know there 3 l
, if split:
>>> s.split('l') ['he', '', 'o wor', 'd']
this result in count of 4. further, have position of each instance of character in string.
the enumerate
built-in helps out here:
>>> s = 'hello world' >>> c = 'l' # letter looking >>> results = [k k,v in enumerate(s) if v == c] >>> results [2, 3, 9]
now have total number of occurrences len(results)
, , positions in string letter occurs.
the final "trick" problem make sure divide float, in order proper result.
working against sample text (stored in s
):
>>> c = 's' >>> results = [k k,v in enumerate(s) if v == c] >>> results_sum = sum(results) >>> (results_sum / len(results)) / float(len(s)) 0.5804132973944295
Comments
Post a Comment