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

Use Python to Load a file containing a list of words as a python list :param...
Use Python to Load a file containing a list of words as a python list :param str filename: path/name to file to load :rtype: list
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)
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...
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...
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...
PYTHON    Generates a list of datetimes given a list of dates and a list of...
PYTHON    Generates a list of datetimes given a list of dates and a list of times. All possible combinations of date and time are contained within the result. The result is sorted in chronological order. For example, Input: >>> timetable([date(2019,9,27), date(2019,9,30)], [time(14,10), time(10,30)]) Output: [datetime(2019,9,27,10,30), datetime(2019,9,27,14,10), datetime(2019,9,30,10,30), datetime(2019,9,30,14,10)] Current code: from datetime import date, time, datetime def timetable(dates, times): lis = []    for c in times: for y in dates: x = f"(datetime{y},{c})" lis.append(x) #doesn't work at all
What python codes should I use to solve this question below? Use input() function once to...
What python codes should I use to solve this question below? Use input() function once to retrieve an input and assign that input value to a variable. Check if the input contains the string “an”. If so, use input() function one more time to retrieve an input and assign the new input value to a different variable. Now, replace “an” characters in the first variable with the first two characters of the second variable if only the second variable has...
Write a code in python to read in a file of spice like component and/or control...
Write a code in python to read in a file of spice like component and/or control data. break the data up into fields in a class object.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT