In Python: Given a file like this: Blake 4 Bolt 1 De Grasse 3 Gatlin 2 Simbine 5
ID: 3785840 • Letter: I
Question
In Python:
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.
Explanation / Answer
from nltk.tokenize import RegexpTokenizer
from nltk.tokenize import word_tokenize
from nltk.tokenize import PunktSentenceTokenizer
data = open("E:Cheggpythoninput.txt",'r')
##reg_tokenizer = RegexpTokenizer("[w']+")
##sent_tokenizer = PunktSentenceTokenizer()
##name = ""
##for i in data:
## for sent in sent_tokenizer.tokenize(i):
## for word in reg_tokenizer.tokenize(sent):
## if word.isalpha():
## print word
l = []
k = []
for i in data:
l.append(i)
for i in l:
sublist = str(i)
k.append(sublist)
name = ""
two_word = 0
> for j in k:
if len(j.split(" ")) == 3:
for i in j.split(" "):
if i.isalpha():
name = name +" "+ i
print name
two_word = two_word + 1
name = ""
else:
for i in j.split(" "):
if i.isalpha():
print i
+ 1
print "Total one word name =" + str (one_word) +" "
print "Total two word name =" + str(two_word)+ " "
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.