Question

I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a...

I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a solution for this item? Thank you. Here is the code I have. I just need 1 instance of each to show in my output. should look like:

AM 3

I 3

SAM 3

Thank you...

# Put your code here
#fileName = input("Enter the file name: ")
fileName = "example.txt"
#fileName = "kgtest.txt"
inputFile = open(fileName, 'r')
text = inputFile.read()
#print(text)
words = text.split()
#print(words)
words.sort()
#print(words)
#theList = []

for word in words:
# print(word)
print(word,words.count(word))
# theDict[word,words.count(word)]
# theList.append(word, words.count(word))
inputFile.close()

there is no comment. not sure what else you need

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

# take input of the sample file from user

fileName=input("Please enter the file name: ").strip()

# declare teh words variable to hold the words from the text file

words=''

# if not valid file please diplay the message

if not fileName:

    print("Please enter a valid file name")

else:

    # open file and read the content to a string words

    f=open(fileName,'r')

    words=f.read()

    f.close()

# declare the dictionary

theDict={}

# split the ords based on and trim them at the same time

words_lst=[x.strip() for x in words.split(',')]

# sort the words lsit we created above,

# its in-place operation. ie, variables are sorted and restructed

words_lst.sort()

# looping over the words in the list

for word in words_lst:

    # print the words in list,

    # along with the times of occurance in the list.

    print(word,words_lst.count(word))

    # add word in the dictionary

    if word not in theDict:

        theDict[word]=words.count(word)

# display the dictionary created form the words list

print(theDict)



SNIPPET:

example.txt:

I, Am, Sam, From, West Virginia, living, in, a, boulevard, 234 Street, in, city, I, find, many, things

RESULT:

Add a comment
Know the answer?
Add Answer to:
I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Below is what I am working. The first two screen caps are from Codio a program we use online at my school. I have been r...

    Below is what I am working. The first two screen caps are from Codio a program we use online at my school. I have been running my code through Python 3 to make sure it works before submitting my assignments; usually I have little to no issues. But now I am stuck on this one problem. Please help. Step 4: Construct confidence interval for population proportion Construct a 99% confidence interval for the proportion of months with rice production above...

  • Consider python please for this problem: I did a python program to read the first 20 lines from a...

    Consider python please for this problem: I did a python program to read the first 20 lines from a file and here is the code holder = 20 lines = 3 file = "123456.pssm" _file = ".txt" while 1:         out_file = input("[OUTPUT] - Please enter the name of file: ") + _file         if (not _file):         print("Filename not specified - Please Try Again\n")         continue     break with open(_file, "a") as e:     with open(file, "r") as f:         for num, line in enumerate(f, 1):                        ...

  • I have the current code that is working. I need to edit it so that I...

    I have the current code that is working. I need to edit it so that I am only counting strings that are after the word 'From' in a text file. Right now it's counting all words, but I just need the From email addresses in a text file. I think the issue if in the if/else loop but nothing I have tried works. Please help! 1 import string 2 fhand open('C: \Users \Brooke\Canopy scripts mbox-short.txt') 3 counts dict( 4 for...

  • I am new to Python and am having trouble coming up with writing code to the...

    I am new to Python and am having trouble coming up with writing code to the following problem... The program must: Prompt for a file name Opens that file and reads through the file Displays a custom error message if the file does not exist You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon. From [email protected] Sat Jan 5 09:14:16 2008 Accumulated the...

  • I have a python project that requires me to make a password saver. The major part...

    I have a python project that requires me to make a password saver. The major part of the code is already giving. I am having trouble executing option 2 and 3. Some guidance will be appreciated. Below is the code giving to me. import csv import sys #The password list - We start with it populated for testing purposes passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]] #The password file name to store the passwords to passwordFileName = "samplePasswordFile" #The encryption key for the caesar...

  • python beginner question! Hello, i am trying to allign the two variable below next to each...

    python beginner question! Hello, i am trying to allign the two variable below next to each other using a for loop, like this table below: 1 3 2 7 3 1 4 2 5 0 heres the code I use: balls = [1,2,3,4,5] count = [3,7,1,2,0] for j in numbers: print("{:}".format(j)) for i in count: print('{:10}'.format(i)) but the output looks like this: 1 2 3 4 5 3 7 1 2 0 i need my output to look like the...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • I am writing python code. I submitted an assignment but the professor said it was a...

    I am writing python code. I submitted an assignment but the professor said it was a modularization. Can you help me see what part of the code is modularization? Pseudocode: 1. Decalre MainMethod () function: # A. Parameters: Three numbers # B. Put the three numbers in reverse # 2. Main Program # A. Initialize Varibles # B. Accepts three values from the user # C. Invoke MainMethod () function, passing the three numbers as arguments in reverse # D....

  • PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK...

    PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK YOU! Bonus Question Implement an efficient algorithm (Python code) for finding the 11th largest element in a list of size n. Assume that n will be greater than 11. det find 11th largest numberlissy Given, lissy, a list of n integers, the above function will return the 11th largest integer from lissy You can assume that length of lissy will be greater than 11....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT