Python Forum
Computer science can you help me with the last part of the code after mentionedWords.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Computer science can you help me with the last part of the code after mentionedWords.
#5
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
Reply


Messages In This Thread
RE: Computer science can you help me with the last part of the code after mentionedWords. - by shirleylam852 - Nov-27-2020, 11:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Having trouble with my Computer Science task Dunxx 1 1,881 Oct-07-2021, 12:32 PM
Last Post: DeaD_EyE
  computer science coursework, read the text please and tell me if theres any specifics sixcray 4 2,800 Nov-11-2020, 03:17 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020