Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary Homework
#1
Hi, I keep getting an error that says "str' object has no attribute 'items" on line 3 and I don't understand why. Can someone help me fix the code? Thanks!

def get_inverted_index(words):
    tweetDict = {}
    wordCount = {}
    for tweetKey, tweetText in words.items():
        for word in tweetText.lower().split():
            wordCount[word]=wordCount.get(word,0)+1
            if inverted_index.get(word,False):
                if tweetKey not in tweetDict[word]:
                    tweetDict[word].append(tweetKey)
            else:
                tweetDict[word] = [tweetKey]
    return tweetDict, wordCount
gettysburg_address = "Four score and.."
print = (get_inverted_index(gettysburg_address))
Reply
#2
gettysburg_address is a str, i.e. "Four score and.."
you need to pass dict as argument when call the function, not str
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You are passing a string to the function on line 15. That string is in the variable words in the function. As the error says, strings don't have items. That's an attribute of a dictionary.

Sniped: buran beat me to it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
also, not related to your problem but still bug
print = (get_inverted_index(gettysburg_address))
should be
print(get_inverted_index(gettysburg_address))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary/List Homework ImLearningPython 22 10,360 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython

Forum Jump:

User Panel Messages

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