Question

In: Computer Science

In Python And Use List comprehension to solve this question Given a file like this: Blake...

In Python And Use List comprehension to solve this question

Given a file like this:

Blake 4

Bolt 1

De Grasse 3

Gatlin 2

Simbine 5

Youssef Meite 6

Your program when completed should produce output like this:

>>>

In the 2016 100 yard dash the top finishers were:

Blake

Bolt

De Grasse

Gatlin

Simbine

Youssef Meite

The people with a two part name are: ['De Grasse', 'Youssef Meite']

The top three finishers were: ['Bolt', 'De Grasse', 'Gatlin']

Your mission will be to do the following:

1. Develop algorithms to solve all remaining steps in this problem.

2. Use a list comprehension to load the data from a file named “runners.txt”.

3. Use the information read in from the file to print out the names of the top 6 finishers formatted as shown in the example above.

4. Use a list comprehension to create a list of the names that have two parts to them.

5. Use a list comprehension to create a list of the people who finished in the top the positions.

Solutions

Expert Solution

Program screenshot:

Sample input file (runners.txt):

Sample output:

Code to copy:

############### code for python 3 #################

#Open the file in read mode.

file=open("runners.txt","r")

#Display the heading.

print("In the 2016 100 yard dash the top finishers were:")

#Declare the lists.

two_part=[]

top_three=[]

#Start the loop till end of file.

for line in file:

    #Split the line

    str1=line.split()

    #Check the length of the list.

    if(len(str1)==2):

        #Display the name.

        print (str1[0])

        #Check the rank of the player.

        if(int(str1[1])<=3):

            #Store the name of the player.

            top_three.append(str1[0])

    #Check the length of the list.

    if(len(str1)==3):

        #Store the name of the player.

        temp=str(str1[0])+" "+str(str1[1])

        #Display the name.

        print(temp)

        #Store the name in the list.

        two_part.append(temp)

        #Check the rank of the player.

        if(int(str1[2])<=3):

            #Store the name of the player.

            top_three.append(temp)

#Display the players with 2 part names.

print ("The people with a two part name are: ",two_part)

#Display the top 3 players.

print ("The top three finishers were: ",top_three)


Related Solutions

Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
python coding Suppose a list of positive numbers is given like the following list (remember this...
python coding Suppose a list of positive numbers is given like the following list (remember this is only an example and the list could be any list of positive numbers) exampleList: 15 19 10 11 8 7 3 3 1 We would like to know the “prime visibility” of each index of the list. The “prime visibility” of a given index shows how many numbers in the list with indexes lower than the given index are prime. For instance, in...
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Bonus Question: Use the provided excel file to solve this question). A small company receives an...
Bonus Question: Use the provided excel file to solve this question). A small company receives an annual order of 800 units which are sold at $85 per unit. The production cost is $30 per unit and the fixed cost per year is $3,500. The initial investment is $ 75,000 and can be depreciated on a MACRS basis over a seven-year period where the marginal income tax rate is 35%. At the end of 4 years, the company is expected to...
Can anyone use python to solve this problem: Part B: List of lists The aim of...
Can anyone use python to solve this problem: Part B: List of lists The aim of Part B is to learn to work with list of lists. You may want to open the file lab05b_prelim.py in the editor so that you can follow the task description. The first part of the file lab05b_prelim.py defines a variable called datasets. The variable datasets is a list of 4 lists. This part consists of 2 tasks. Task 1: We ask you to type...
Using Python The script is to open a given file. The user is to be asked...
Using Python The script is to open a given file. The user is to be asked what the name of the file is. The script will then open the file for processing and when done, close that file. The script will produce an output file based on the name of the input file. The output file will have the same name as the input file except that it will begin with "Analysis-". This file will be opened and closed by...
python question: Problem Statement Given a list of integers input_list, loop through every element in the...
python question: Problem Statement Given a list of integers input_list, loop through every element in the list to find the product of all positive integers and the count of all negative integers. The code to get the input_list is provided for you. The first line of code provided gets the size of the list. The remaining lines of code provided get the elements of the list. The provided data preprocessing code reads in these values and creates a list of...
PYTHON #What you need to do is to transform the file Mkt_data_test.txt #to the format like...
PYTHON #What you need to do is to transform the file Mkt_data_test.txt #to the format like in file in the screenshot # #Your steps to get there: #1.Create a header line with following columns: Time;Bid\Ask;Price;Volume. NOTE NO SPACES #2.Remove all of the timestamp lines, i.e ======== Data: ..... #3.Remove the 1900-01-01 from the timestamp but leave the time itself #4.Get rid of all spaces and empty lines #5.Replace 0 or 1 in the second position with Bid or Ask, Bid...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT