Using dicts to work with words into a file text

Source: Deep Learning on Medium

Using dicts to work with words into a file text

Hello everybody, in the last post, we talk a little about reading a text file. We do a simple exercise explaining each line. This time we will see some other activities with more code, and I may not be able to explain line by line but explain the idea. For you, if you want to see the result of every line for better comprehension, use jupyter notebook instead VSCode that I am going to use.

Programming is the conjugation of type of objects, reserved words, and the logic that we can perform using these things to fulfill our needs. So, we need to use our code jointed with code that the language has to help us in our tasks. We are going to see some of that now.

This time, we are going to allow the user input with the idea of him being able to enter other files. For this, we need to have a default file in case he doesn’t choose any

Dictionaries are famous, and almost every Programmation language has an implementation for it. It allows us to save data using a key and value pair that makes it easy to use those values when needed and have the code more organized and readable than using lists. This time we want to count how many times a word is used in the text we are working. For this, we will use the specific word as key, and we will save in the value the amount of the apparition summing 1 into the value if the key already exists in the dictionary.

Above, we will have a dictionary with every word of the file as key and its apparition amount in its value. Let’s do some other thing. We are going to find which is the word with more apparition and the count of those apparitions.

Let’s print the result.

Not let’s see a more specified print with every word and its counter.


For training, you can try with other files and check the results.

Thanks for reading