Reading a text file to print frequency of letters in decreasing order - Python 3

print characters in decreasing order of frequency in c
python count letters in text file
determining frequency of words in text file python

I am doing python basic challenges this is one of them. What all I needed to do is to read through a file and print out the frequency of letters in decreasing order. I am able to do this but I wanted to enhance the program by also printing out the frequency percentage alongside with the letter - frequency - freq%. Something like this: o - 46 - 10.15%

This is what I did so far:

def exercise11():
    import string
    while True:
        try:
            fname = input('Enter the file name -> ')
            fop = open(fname)
            break
        except:
            print('This file does not exists. Please try again!')
            continue

    counts = {}
    for line in fop:
        line = line.translate(str.maketrans('', '', string.punctuation))
        line = line.translate(str.maketrans('', '', string.whitespace))
        line = line.translate(str.maketrans('', '', string.digits))
        line = line.lower()
        for ltr in line:
            if ltr in counts:
                counts[ltr] += 1
            else:
                counts[ltr] = 1
    lst = []
    countlst = []
    freqlst = []
    for ltrs, c in counts.items():
        lst.append((c, ltrs))
        countlst.append(c)
    totalcount = sum(countlst)
    for ec in countlst:
        efreq = (ec/totalcount) * 100
        freqlst.append(efreq)
    freqlst.sort(reverse=True)
    lst.sort(reverse=True)
    for ltrs, c, in lst:
        print(c, '-', ltrs)

exercise11()

As you can see I am able to calculate and sort the freq% on a different list but I am not able to include it in the tuple of the lst[] list alongside with the letter, freq. Is there any way to solve this problem?

Also if you have any other suggestions for my code. Please do mention. Output Screen


Modification

Applying a simple modification as mentioned by @wwii I got the desired output. All I had to do is add one more parameter to the print statement while iterating the lst[] list. Previously I tried to make another list for the freq%, sort and then tried to insert it to the letters-count tuple in a list which didn't work out.

 for ltrs, c, in lst:
        print(c, '-', ltrs, '-', round(ltrs/totalcount*100, 2), '%')

Output Screen

Your count data is in a dictionary of {letter:count} pairs.

You can use the dictionary to calculate the total count like this:

total_count = sum(counts.values())

Then don't calculate the percentage till you are iterating over the counts...

for letter, count in counts.items():
    print(f'{letter} - {count} - {100*count/total}')    #Python v3.6+
    #print('{} - {} - {}'.format(letter, count, 100*count/total)    #Python version <3.6+

Or if you want to put it all in a list so you can sort it:

data = []
for letter, count in counts.items():
    data.append((letter,count,100*count/total)

Using operator.itemgetter for the sort key function can help code readability.

import operator
letter = operator.itemgetter(0)
count = operator.itemgetter(1)
frequency = operator.itemgetter(2)

data.sort(key=letter)
data.sort(key=count)
data.sort(key=frequency)

Solved: Letters in decreasing order of frequency, Reading a text file to print frequency of letters in decreasing order in python 3+ zipped = zip(lst, countlst, freqlst) zipped = sorted(zipped,  I am doing python basic challenges this is one of them. What all I needed to do is to read through a file and print out the frequency of letters in decreasing order. I am able to do this but I wanted to enhance the program by also printing out the frequency percentage alongside with the letter - frequency - freq%. Something like this: o - 46

Tuples are immutable which is probably the issue you are finding. The other issue is the simple form of the sort function; A more-advanced sort function would serve you well. See below:

The list-of-tuples format of lst, but because tuples are immutable whereas lists are mutable, opting to change lst to a list-of-lists is a valid approach. Then, since lst is a list-of-lists with each element consisting of 'letter,count,frequency%', the sort function with lambda can be used to sort by whichever index you'd like. The following is to be inserted after your for line in fop: loop.

lst = []
for ltrs, c in counts.items():
    lst.append([ltrs,c])
totalcount = sum([x[1] for x in lst])       # sum all 'count' values in a list comprehension

for elem in lst:
    elem.append((elem[1]/totalcount)*100)   # now that each element in 'lst' is a mutable list, you can append the calculated frequency to the respective element in lst

lst.sort(reverse=True,key=lambda lst:lst[2])    # sort in-place in reverse order by index 2.

Python, "Write a program that reads a file and prints the letters The behavior of strings changed between Python 2 and Python 3; the tuple() line is likely necessary for  Hey everone, so I had this exercise : "Write a program that reads a file and prints the letters in decreasing order of frequency. Your program should convert all the input to lower case and only count the letters a-z. Your program should not count spaces, digits, punctuation, or anything other than

The items in freqlst,countlist, and lst are related to each other by their position. If any are sorted that relationship is lost.

zipping the lists together before sorting will maintain the relationship.

Will pick up from your list initialization lines.

lst = []
countlst = []
freqlst = []
for ltr, c in counts.items():
    #change here, lst now only contains letters
    lst.append(ltr)
    countlst.append(c)
totalcount = sum(countlst)
for ec in countlst:
    efreq = (ec/totalcount) * 100
    freqlst.append(efreq)

#New stuff here: Note this only works in python 3+
zipped = zip(lst, countlst, freqlst)
zipped = sorted(zipped, key=lambda x: x[1])

for ltr, c, freq in zipped:
    print("{} - {} - {}%".format(ltr, c, freq)) # love me the format method :)

Basically, zip combines lists together into a list of tuples. Then you can use a lambda function to sort those tuples (very common stack question)

Think Python: How to Think Like a Computer Scientist, By using our site, you acknowledge that you have read and understood our with exactly two bits set · Python: Convert Speech to text and text to Speech Given a string, the task is to find the frequencies of all the characters in that Python3 code to demonstrate print ( "Count of all characters in GeeksforGeeks is :\n ". Sample Execution: python timeofday.py Enter a file name: mbox-short.txt 04 3 06 1 07 1 09 2 10 3 11 6 14 1 15 2 16 4 17 2 18 1 19 1 Exercise 3 Write a program that reads a file and prints the letters in decreasing order of frequency.

I think I was able to achieve what you wanted by using lists instead of tuples. Tuples cannot be modified, but if you really want to know how click here

(I also added the possibility to quit the program)

Important: Never forget to comment your code

The code:

def exercise11():
    import string
    while True:
        try:

            fname = input('Enter the file name -> ')
            print('Press 0 to quit the program') # give the User the option to quit the program easily
            if fname == '0':
                break
            fop = open(fname)
            break
        except:
            print('This file does not exists. Please try again!')
            continue

    counts = {}
    for line in fop:
        line = line.translate(str.maketrans('', '', string.punctuation))
        line = line.translate(str.maketrans('', '', string.whitespace))
        line = line.translate(str.maketrans('', '', string.digits))
        line = line.lower()
        for ltr in line:
            if ltr in counts:
                counts[ltr] += 1
            else:
                counts[ltr] = 1
    lst = []
    countlst = []
    freqlst = []

    for ltrs, c in counts.items():
        # add a zero as a place holder & 
        # use square brakets so you can use a list that you can modify 
        lst.append([c, ltrs, 0]) 
        countlst.append(c)
    totalcount = sum(countlst)

    for ec in countlst:
        efreq = (ec/totalcount) * 100
        freqlst.append(efreq)
    freqlst.sort(reverse=True)
    lst.sort(reverse=True)

    # count the total of the letters 
    counter = 0
    for ltrs in lst:
        counter += ltrs[0]

    # calculate the percentage for each letter 
    for letter in lst:
        percentage = (letter[0] / counter) * 100
        letter[2] += float(format(percentage, '.2f'))

    for i in lst:
        print('The letter {} is repeated {} times, which is {}% '.format(i[1], i[0], i[2]))
exercise11()

Print characters in decreasing order of frequency, that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies word list from a file (see “Reading Word Lists”) and prints all the sets of words that 3. In Scrabble, a “bingo” is when you play all seven tiles in your rack​, along  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.

<?php

$fh = fopen("text.txt", 'r') or    die("File does not exist");
 $line = fgets($fh); 

 $words = count_chars($line, 1); 

foreach ($words as $key=>$value)
   {
   echo "The character  <b>' ".chr($key)." '</b>  was found   <b>$value</b>   times. <br>";
   }

?>

Counting Word Frequencies with Python, Given a string str, the task is to print the characters in decreasing order of their frequency. If the frequency of two characters is same then sort them in descending order Print all distinct characters of a string in order (3 Methods) · Print common Python code to print common characters of two Strings in alphabetical order  Given a string, the task is to find the frequencies of all the characters in that string and return a dictionary with key as the character and its value as its frequency in the given string. Simply iterate through the string and form a key in dictionary of newly occurred element or if element is already occurred, increase its value by 1.

Letter frequency, Counting the frequency of specific words in a list can provide Files Needed For This Lesson +"\n") print("List\n" + str(wordlist) + "\n") print("Frequencies\n" + Sort a dictionary of word-frequency pairs in # order of descending response = urllib.request.urlopen(url) html = response.read() text = obo. Finally, in order to get the word and its frequency (number of times it appeared in the text file), we can do the following: for words in frequency_list: print words, frequency[words] Let's put the program together in the next section, and see what the output looks like.

Counting Word Frequency in a File Using Python, freq 0 1 2 3 2 3 4 3 4 4 4 0 1 1 1 Read the file as an NSString, letting the system guess the encoding. Derive an array of dictionaries from the list and sort it on the letters. This function prints the Letter frequency of a given textfile. Sort on decreasing occurrences: letters = string.ascii_lowercase # Python 2.2 and later A Python program can read a text file using the built-in open() function. For example, below is a Python 3 program that opens lorem.txt for reading in text mode, reads the contents into a string variable named contents , closes the file, and then prints the data.

Exercises in Programming Style, In this tutorial, we are going to use test.txt as our test file. 1. text_string = document_text.read().lower() that would return all the words with the number of characters in the range [3-15] . Finally, in order to get the word and its frequency (number of times it appeared in the print words, frequency[words]  First you should open your file so you can read it. So let’s say the file is in your working directory, you open it as follows: myFile = open(“textFile.txt”, “r+”) Then you create a dictionary to keep track of all the letters: d = {} Now you iterate over your file’s text, and every time you find a letter, you add it to the dictionary and assign a value of 1 to it.

Comments
  • Do you have to use a tuple? If you simply use a list you shouldn't encounter this issue. Tuples are not mutable
  • @NahuelP Yes, to sort the freq I need to have a tuple inside a list to co-relate the freq and the letter.
  • so is @wwi's answer correct?
  • @EzizDurdyyev Yes it is also, its the simplest modification to resolve my issue.
  • @SrijanSingh so you should accept that answer
  • Not sure why we need to get hung up on the imutability of tuples. The len of the lists will all be the same. Just use zip() to get them together. That said, advanced sorting is the way to go here for sure :) We are in agreement there. Cheers
  • The only flaw with this is that it prints freq(counts) two times in the output screen, once while within the tuple inside the lst[] list and second with the countlst list.
  • yeah sorry. That is super easy to fix tho by removing the tuple creation in the first for loop. I was trying to illustrate a solution. I will fix. If this helped, consider accepting it
  • Code examples are welcome but please also explain your solution
  • this code is for reading the the number of characters and how offten they are repeated in the file.