Nov-27-2020, 11:18 PM
(This post was last modified: Nov-27-2020, 11:18 PM by shirleylam852.)
def cleanedup(s): alphabet='abcdefghijklmnopqrstuvwxyz@_0123456789' cleantext= '' for character in s.lower(): if character in alphabet: cleantext+= character else: cleantext+=' ' return cleantext import os def contains(filename): counts={} mentionedWords=[] with open(filename) as file: for word in cleanedup(line).split(): if filename[2] =='@': if word in counts: counts[word]+=1 else: counts[word]=1 print("fn=",filename) for word in counts: mentionedWords.append([counts[word],word]) for filename in os.listdir('.'): if filename[-3:]=='.tweets': contains (filename) mentionedWords.sort() for item in mentionedWords[-3:]: print (' ', item [1], item[0])@perfringo
Okay, my code as of right now. I'm missing something in between line 32-36.
The only hint I was given to fix my code was this:
To print the username followed by its count, remember that each item in mentionedWords in your program is a list, and the unit 4 "Learn" PDF starting on p. 8 explains that items in a list can be accessed by index. For example, suppose you have a list x = [123, 'abc']. Then, x[0] is 123 and x[1] is 'abc'. If you want to print the second item in the list followed by the first item, i.e. abc followed by 123, you would just do print(x[1], x[0]). So, in your program, write a loop that goes through each item of the slice that represents the 3 most frequently mentioned usernames in mentionedWords.
If code was written correctly output should run like this:
nytimes.tweets
@caityweaver 3
@nytmag 5
@nytparenting 5
justinbieber.tweets
@applemusic 15
@theellenshow 15
@skrillex 20
aoc.tweets
@rashidatlaib 5
@ayannapressley 6
@ilhanmn 9
espn.tweets
@nba 21
@thecheckdown 29
@kingjames 32
rihanna.tweets
@rihanna 21
@savagexfenty 29
@fentybeauty 48
amyschumer.tweets
@bridgeteverett 14
@rachelfeinstein 15
@comedycentral 49
ladygaga.tweets
@ahsfx 10
@btwfoundation 11
@applemusic 13
BillGates.tweets
@theeconomist 11
@warrenbuffett 15
@melindagates 18
BarackObama.tweets
@ofa 5
@vp 5
@michelleobama 9
ID_AA_Carmack.tweets
@boztank 3
@JoeRogan 3
@elonmusk 5
Kaepernick7.tweets
@mikailsprice 26
@darthkaepernick 28
@kaepernick7 138
doctorow.tweets
@cbc 3
@doctorow 3
@sensanders 7