Python Forum
Dictionary + File Reading
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary + File Reading
#1
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.

Attached Files

.zip   Dictionary+Article.zip (Size: 442.82 KB / Downloads: 601)
Reply
#2
(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
Recommended Tutorials:
Reply
#3
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,132 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,452 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Excel File reading vanjoe198 1 2,007 Mar-31-2021, 11:53 AM
Last Post: snippsat
  Updating dictionary in another py file tommy_voet 1 4,782 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,481 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,209 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,369 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  reading from a file looseCannon101 14 4,754 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Weird problem with reading from file and performing calculations pineapple999 1 2,956 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 4,719 Jul-18-2019, 01:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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