Python Forum
Dictionary + File Reading - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Dictionary + File Reading (/thread-937.html)



Dictionary + File Reading - palmtrees - Nov-15-2016

I have no idea how to do this program.

I have to write a program that creates a dictionary of words, which is stored in a file. Then that program has to read another file to see if words are in the dictionary. The output is how many times the word appears, the length of the word, and how many constants it has.

I have attached the dictionary and article I have been given.

Please help, thank you so much.


RE: Dictionary + File Reading - metulburr - Nov-15-2016

(Nov-15-2016, 03:48 PM)palmtrees Wrote: I have no idea how to do this program.

I have to write a program that creates a dictionary of words, which is stored in a file. Then that program has to read another file to see if words are in the dictionary. The output is how many times the word appears, the length of the word, and how many constants it has.

You need to know how to read/write to/from a file
loop each line by newline
you can get length of a string via builtin len
you can get the constant count of the word by str.count
counting word frequencies


RE: Dictionary + File Reading - Ofnuts - Nov-15-2016

(Nov-15-2016, 03:48 PM)palmtrees Wrote: I have no idea how to do this program.

I have to write a program that creates a dictionary of words, which is stored in a file. Then that program has to read another file to see if words are in the dictionary. The output is how many times the word appears, the length of the word, and how many constants it has.

I have attached the dictionary and article I have been given.

Please help, thank you so much.

I have to write a program that creates a dictionary of words, which is stored in a file.

From what I see in the attached zip, you have to read a dictionary file and create a "list" of words in a python variable. This "list" is a likely not a "dictionary" in the Python sense but either a plain Python list, or better a Python set.

Then that program has to read another file to see if words are in the dictionary.

So you read the article file, split it on words, and then for each word, check if the word in the the big list (but you don't output that result?)

The output is how many times the word appears, the length of the word, and how many constants it has.

So you have to count the words. So you have to maintain a structure that relates a string (the word you extracted from the article) to the count of its instances. In Python, this is a "dictionary" (where the key is the word, and the value is the count of instances). When you encounter a word you check if it is in the instances-count dictionary, and if so increment the count, otherwise add it to the dictionary with a count of 1. And when you have read the whole article, all the words have become keys in the dictionary. So you can iterate the dictionary keys, and for each: print the key, print the count, print the length, and print the result of a function that will count the consonants in the key.