Python Forum
[Help] How to count Letter frequency in a text file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] How to count Letter frequency in a text file?
#1
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. :)

==============================================================================
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

.txt   alice_in_wonderland.txt (Size: 169.53 KB / Downloads: 565)
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply


Messages In This Thread
[Help] How to count Letter frequency in a text file? - by vanicci - Aug-07-2018, 12:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,133 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Row Count and coloumn count Yegor123 4 1,338 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,696 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,018 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  all i want to do is count the lines in each file Skaperen 13 4,847 May-23-2021, 11:24 PM
Last Post: Skaperen
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,376 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  How to use the count function from an Excel file using Python? jpy 2 4,470 Dec-21-2020, 12:30 AM
Last Post: jpy
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,422 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  get two characters, count and print from a .txt file Pleiades 9 3,409 Oct-05-2020, 09:22 AM
Last Post: perfringo
  saving data from text file to CSV file in python having delimiter as space K11 1 2,413 Sep-11-2020, 06:28 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020