Aug-07-2018, 12:38 PM
Hello,
I tried looking for "letter frequency" or "frequency distribution" within the forum but I couldn't find any old thread about the subject, unfortunately.
Here's the task that I'm trying to do:
Calculate a table for each letter in the alphabet from a-z, and count how many times each letter appears in alice_in_wonderland.txt (fancy word for counting stuff is "frequency distribution" - because you are counting the frequency of something)
a: 34,560
b: 5,027
...
z: 893
Store the results in a list of lists:
result = [
["a", 34560],
["b", 5027],
...
["z", 893]
]
Hint: Use python's lower() method to turn all alphabetic letters into small case and count them (so "A" counts towards "a"). Ignore non-alphabetic numbers, you can check with python isalpha() method.
==============================================================================
See attached alice_in_wonderland.txt file in this thread so you can play around with it?
As always, any feedback, as well as ideas, is much appreciated from any Python experts here.
Thank you kindly. :)
==============================================================================
Screenshot of my Terminal:
[Image: 07080-AinW_zpsga41asnk.png]
I tried looking for "letter frequency" or "frequency distribution" within the forum but I couldn't find any old thread about the subject, unfortunately.
Here's the task that I'm trying to do:
Calculate a table for each letter in the alphabet from a-z, and count how many times each letter appears in alice_in_wonderland.txt (fancy word for counting stuff is "frequency distribution" - because you are counting the frequency of something)
a: 34,560
b: 5,027
...
z: 893
Store the results in a list of lists:
result = [
["a", 34560],
["b", 5027],
...
["z", 893]
]
Hint: Use python's lower() method to turn all alphabetic letters into small case and count them (so "A" counts towards "a"). Ignore non-alphabetic numbers, you can check with python isalpha() method.
==============================================================================
See attached alice_in_wonderland.txt file in this thread so you can play around with it?
As always, any feedback, as well as ideas, is much appreciated from any Python experts here.
Thank you kindly. :)
==============================================================================
from collections import defaultdict filename = "alice_in_wonderland.txt" file = open(filename, encoding="utf8") def countletters(file): results = defaultdict(int) for line in file: for char in line: if char.lower() in filename: c = char.lower() results[c] += 1 return results print(countletters(file))==============================================================================
Screenshot of my Terminal:
[Image: 07080-AinW_zpsga41asnk.png]
Attached Files
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube