Question

In: Computer Science

PYTHON. Create a function that accepts a filename where in each line there is a name...

PYTHON. Create a function that accepts a filename where in each line there is a name of a type of bird. use a python dictionary in order to count the # of time each bird appears. (1 line = 1 appearance) Loop over your dict. to print each species and the # of times it was seen in the file once all lines have been counted. return dictionary containing count

the filename opens this:

blackbird

canary

hummingbird

canary

hummingbird

canary

example of outcome. Blackbird,1

Hummingbird,2

Canary,3

Solutions

Expert Solution

Here is the code in python.

filename = input("Enter filename: ")

# reading all lines from the file and adding to a list
with open(filename,'r') as f:
    all_lines = f.readlines()

for i in range(len(all_lines)):
    # removing \n from the end each line in the list
    all_lines[i] = all_lines[i].split("\n")[0]

# creating an empty dictionary
birdsCount = {}

# adding all birds to birdsCount if they are already not present
for bird in all_lines:
    if bird not in birdsCount:
        birdsCount[bird] = 0

# counting the occurence of each bird
for bird in all_lines:
    birdsCount[bird]+=1
# printing the name of each bird and its count
for bird in birdsCount:
    print("{},{}".format(bird,birdsCount[bird]))

I have added some comments to make you understand better

OUTPUT:

If you like the answer, please upvote.


Related Solutions

(IN PYTHON) Write a function that accepts a line of text and a single letter as...
(IN PYTHON) Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the last character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter.
Language: Python 3 Compose a function process which accepts a string filename. process should return a...
Language: Python 3 Compose a function process which accepts a string filename. process should return a list of records contained in the file. #define your function here # Create a blank list called `entries` and an empty string called `current_style`. # Open the file `filename`, read the data using readlines(), and close it. # Loop through each line of the file and do the following: # Strip the whitespace off of the ends of the line using the `strip` method....
Create a new Python program (you choose the filename) that contains a main function and another...
Create a new Python program (you choose the filename) that contains a main function and another function named change_list. Refer to the SAMPLE OUTPUT after reading the following requirements. In the main function: create an empty list. use a for loop to add 12 random integers, all ranging from 50 to 100, to the list. use second for loop to iterate over the list and display all elements on one line separated by a single space. display the 4th element,...
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the...
#Write a function called "load_file" that accepts one #parameter: a filename. The function should open the #file and return the contents.# # # - If the contents of the file can be interpreted as # an integer, return the contents as an integer. # - Otherwise, if the contents of the file can be # interpreted as a float, return the contents as a # float. # - Otherwise, return the contents of the file as a # string. #...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
Process each line so you create dictionary where key is name of the company and value...
Process each line so you create dictionary where key is name of the company and value is a net balance #2.a. First column of each row is a name of the company #2.a.a. Utes company has different types of spelling, so you need to put all of them under the same key #2.b. Second column is payments in #2.c. Third column is payments out. Note that they can be negatives as well. # Hint: Absolute value could be derived by...
Python. Create a function that receives the name of an auto maker and returns a slice...
Python. Create a function that receives the name of an auto maker and returns a slice of the dataframe with the cars from that auto maker. For instance, it may receive 'ford' and return a slice with all the cars produced by 'ford' meaning they have 'ford' in their name. Call the function for 'ford' to see if it works properly. Hint: You may create a list comprehension producing a list of all the index labels that include the auto-maker's...
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT