Question

I need to,  Modify my mapper to count the number of occurrences of each character (including punctuation...

I need to,  Modify my mapper to count the number of occurrences of each character (including punctuation marks) in the file.

Code below:

#!/usr/bin/env python

#the above just indicates to use python to intepret this file

#This mapper code will input a line of text and output <word, 1> #

import sys

sys.path.append('.')

for line in sys.stdin:

line = line.strip() #trim spaces from beginning and end

keys = line.split() #split line by space

for key in keys:

value = 1

print ("%s\t%d" % (key,value)) #for each word generate 'word TAB 1' line

Homework Answers

Answer #1

Assuming you need the number of occurrences for each character (not word). We use python's dictionary data structure to hold the unique characters and their counts. Iterate through each character in the line and add them into the dictionary with count 1 if not present in the dictionary. If the character is already present, increment its count by 1. At the end of the loop, you will have the total number of occurrences for all unique characters in the dictionary.

count_dict ={} # Initialize an empty dictionary. If you need to process multiple lines, do this inside a loop
for char in line: # Iterate through each character in the line
if char in count_dict: # If the character is already present in the dictionary, increment its value by 1
count_dict[char] = count_dict[char] + 1
else: # Else if it is a new character, add it into the dictionary with value as 1
count_dict[char] = 1

# Iterate through the dictionary and print the key- value pairs. Character is the key & count is the value in the dictionary

for key,value in count_dict.items():
print("%s\t%d" % (key,value))

Know the answer?
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
Similar Questions
Modify your mapper to count the number of occurrences of each character (including punctuation marks) in...
Modify your mapper to count the number of occurrences of each character (including punctuation marks) in the file. #!/usr/bin/env python #the above just indicates to use python to intepret this file #This mapper code will input a line of text and output <word, 1> # import sys sys.path.append('.') for line in sys.stdin: line = line.strip() #trim spaces from beginning and end keys = line.split() #split line by space for key in keys: value = 1 print ("%s\t%d" % (key,value)) #for...
Modify your mapper to count words after removing punctuation marks during mapping. Mapper code below. #!/usr/bin/env...
Modify your mapper to count words after removing punctuation marks during mapping. Mapper code below. #!/usr/bin/env python #the above just indicates to use python to intepret this file #This mapper code will input a line of text and output <word, 1> # import sys sys.path.append('.') for line in sys.stdin: line = line.strip() #trim spaces from beginning and end keys = line.split() #split line by space for key in keys: value = 1 print ("%s\t%d" % (key,value)) #for each word generate...
I need to. Modify my mapper to count words after removing punctuation marks during mapping. mapper...
I need to. Modify my mapper to count words after removing punctuation marks during mapping. mapper is below: import sys sys.path.append('.') for line in sys.stdin: line = line.strip() #trim spaces from beginning and end keys = line.split() #split line by space for key in keys: value = 1 print ("%s\t%d" % (key,value))
How do I fix my code to collect ONLY hashtag instead of accumulating all the information...
How do I fix my code to collect ONLY hashtag instead of accumulating all the information about the number of occurrences of all words from the textfile? Please help to fix below codes. tweetcount=0 maxcount=0 count = 0 with open('elon-musk.txt') as book: for tweet in book: count += 1 print("Number of tweets:", count) print() with open('elon-musk.txt') as book: for line in book: s count = len(line.split()) if count>maxcount: maxline = line maxcount = count tweetcount += 1 print("Tweet with max...
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT