1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
a_sh-v [17]
3 years ago
5

Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file co

ntains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates.

Computers and Technology
1 answer:
DaniilM [7]3 years ago
3 0

Answer:

Here is the Python code.

import csv  

inputfile = input("Enter name of the file: ")

count = {}

with open(inputfile, 'r') as csvfile:

 csvfile = csv.reader(csvfile)

 for line in csvfile:

   for words in line:

     if words not in count.keys():

       count[words] = 1

     else:

         count[words] + 1

   print(count)

Explanation:

I will explain the code line by line.

import csv   here the css is imported in order to read the file using csv.reader() method

inputfile = input("Enter name of the file: ")  This statement takes the name of the input file from the user

count = {}  this is basically a dictionary which will store the words in the file and the number of times each word appears in the file.

with open(inputfile, 'r') as csvfile:  This statement opens the inputfile using open() method in read mode. This basically opens the input file as csv type

 csvfile = csv.reader(csvfile)  This statement uses csv.reader() method to read the csv file contents.

for line in csvfile:  This outer for loop iterates through each line of the file

for words in line:  This inner loop iterates through each word in the line

if words not in count.keys():  This if condition checks if the word is not in the dictionary already. The dictionary holds key value pair and keys() method returns a list of all the keys of dictionary

count[words] = 1  if the word is not present already then assign 1 to the count dictionary

co unt[words] + 1 if the word is already present in dictionary increment count of the words by 1. Suppose the input file contains the following words:

hello,cat,man,hey,dog,boy,Hello,man,cat,woman,dog,Cat,hey,boy

Then because of co unt[words] + 1 statement if the word appears more than once in the file, then it will not be counted. So the output will be:

{' cat': 1, ' man': 1, ' dog': 1, ' woman': 1, ' Hello': 1, ' hey': 1, ' boy': 1, 'hello': 1, ' Cat': 1}

But if you want the program to count those words also in the file that appear more than once, then change this co unt[words] + 1 statement to:  

count[words] = count[words] + 1

So if the word is already present in the file its frequency is increased by 1 by incrementing 1 to the count[words]. This will produce the following output:

{' Cat': 1, ' cat': 2, ' boy': 2, ' woman': 1, ' dog': 2, ' hey': 2, ' man': 2, ' Hello': 1, 'hello': 1}  

You can see that cat, boy, dog and hey appear twice in the file.                    

print(count) This statement prints the dictionary contents with words and their frequencies.

The program with its output is attached.

You might be interested in
What are the uses of navigation keys​
Zolol [24]
The navigation keys allow you to move the cursor, move around in documents and webpages, and edit text.
3 0
2 years ago
CodeHS Python Rainforest Exercise 5.4.7: Categories
Butoxors [25]

Answer:

List1=[['Physics','Quantum Physics','Theory of Relativity'],['Geometry', 'Plane Geometry', 'Coordinate Geometry']]

i=0

k=0

for i in range(0,2):

   print("Category:"+List1[i][0])

   for k in range(1,3):

       print("\t"+List1[i][k])

   

Explanation:

The program required is mentioned above. And it has limitations, but is good enough to explain the fundamentals required.

6 0
4 years ago
When entering new data into your table, which key should you press to move from one field or cell to the next?
prohojiy [21]

Answer:

Tab key or Enter key is the correct answer.

Explanation:

When the user want to move from one cell to another cell or field then they use the tab key or enter key. If the user wants to fill data vertically then, they use Enter key to move one cell to another and id they wants to fill data horizontally then, they use Tab key to move on the next cell. So, that's why when they want to fill data in Row or Column then, they use tab key or enter key.

6 0
4 years ago
Which of the following offers a combination of the features of ram and rom?
IRISSAK [1]

Cache Flash memory offers a combination of the features of ram and rom.

Memory made of cache flash combines ROM and RAM functionalities. It can be upgraded to store fresh information, just like RAM. It retains the data even if the power to the computer system is turned off, unlike ROM.

Non-volatile computer memory that may be electrically erased and reprogrammed is called flash memory.

• This technology is mainly utilized in memory cards and USB flash drives (also known as thumb drives, handy drives, memory sticks, flash drives, jump drives, and "Cap N' Go") for broad data storage and transmission between computers and other digital devices.

• It is a particular kind of EEPROM (Electrically Erasable Programmable Read-Only Memory) that can be programmed and wiped in big chunks.

• PDAs (personal digital assistants), laptop computers, digital music players, digital cameras, and mobile phones are a few examples of applications.

• It has also become more common in the market for video game consoles, where it is frequently utilized for game save data instead of EEPROMs or battery-powered SRAM.

Know more about flash memory here:

brainly.com/question/13014386

#SPJ4

5 0
1 year ago
What are some examples for Environmental Technology? Also some negative and positive effects?
lakkis [162]
Wave energy and wind power (also hydroelectric dams as well), they create energy without pollution but need a lot of maintenance and some people disagree with their aesthetic influence of the countryside as they are very industrial looking  <span />
5 0
3 years ago
Other questions:
  • What is print media?
    14·1 answer
  • How can styles be used in Word? Check all that apply. to standardize the font size of a title in a Word document to standardize
    9·2 answers
  • You have no control over who views your social network information
    13·2 answers
  • Which method would provide you with pictures you could upload from a disc that you insert in your computer?
    8·1 answer
  • Rachel wants to post content from digital cameras on the web. Which online tool will help Rachel to post and edit content on a w
    5·1 answer
  • Which line of code will print Python is cool! on the screen?
    9·2 answers
  • Https://intro.edhesive.com/courses/17288/assignments/2313298?module_item_id=4864185
    9·1 answer
  • Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards
    11·1 answer
  • What is the combination of the mechanical or digital process of film production and the illusion or illusions created in the min
    12·1 answer
  • I'll give u 16 points answer thisss
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!