In: Computer Science
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.
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)