0

I wanna make word count and list how many times word counted. But


 f = open("Les.Miserable.txt", 'r')

 words = f.read().split()
 words.sort()
 wordCount = ()

 for i in range(len(words)):
     words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
     words[i] = words[i].upper()
     if words[i] not in wordCount:
         wordCount[words[i]] = 1 
     else:
         wordCount[words[i]] += 1

i can see error message 'tuple' object has no attribute 'upper' in

words[i] = words[i].upper()

here

and also error message 'tuple' object does not support item assignment in

wordCountint[words[i]] = 1

Please let me know know what is the problem

5
  • you can add print before statement to find out type of object. Commented May 17, 2015 at 9:17
  • i find out what type it is by print(type(words[i])) = class str , but it doesn't work Commented May 17, 2015 at 9:22
  • Why are making wordCount a tuple not a list? Commented May 17, 2015 at 9:38
  • find out what type is words not words[i] Commented May 17, 2015 at 9:39
  • type(words) is list type. I still confused.. Commented May 17, 2015 at 9:43

3 Answers 3

1

If you print the value of words[i] after your attempted character replacements you will see that it is set to a tuple, e.g.

('word', (',', ''), ('/', ''), ('?', ''), ('!', ''))

So the line that tries to remove unwanted punctuation actually creates a tuple because that's what the comma separated items are, i.e.

words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")

is actually a tuple consisting of words[i].replace(".", "") followed by (",", ""), etc.

You might have meant to chain a whole lot of replace operations together, but that would need to look like this:

words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")

But that is pretty ugly, and it's restricted to just a few punctuation symbols. str.translate() is better:

words[i] = words[i].translate(None, '.,/?!')

or, if you want to get rid of all punctuation you can use string.punctuation:

import string
words[i] = words[i].translate(None, string.punctuation)

Or, if you are using Python 3:

import string
words[i] = words[i].({ord(c):None for c in string.punctuation})

There are other problems in your code, but see if you can correct this first issue first.

Sign up to request clarification or add additional context in comments.

1 Comment

I changed to what you commented but there is another error on wordCount[words[i]] = 1 . Error says 'tuple' object does not support item assignment. could i know what is wrong? and i questioned wrong like wordCountint[words[i]] = 1 but wordCount[words[i]] = 1 is right
0

in this line:

words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")

you assign tuple into words[i]. i guess you want to replace several character and that you mean to do this:

words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")

several values with comma between them are tuple. 1,5,6 is the same as (1,5,6) so
words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")
is the same as
(words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", ""))

in addition, you can't assign into tuple, therefore. the line

wordCount[words[i]] = 1 

can throw an exception, you need to change wordCountint to a dict (when you create it):

wordCount = {}

8 Comments

Thank you for answering. I have changed it but it still error on here wordCount[words[i]] = 1 TypeError: 'tuple' object does not support item assignment
I replaced like this words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "") but there is another error on if words[i] not in wordCount: wordCount[words[i]] = 1 and error says 'tuple' object does not support item assignment
change wordCount = () to wordCount = [] (you can't assign into tuple, however you can into list)
i changed to [] and error says : list indices must be integers, not str
oops, you are right, I meant dictionary. change to {}
|
0
words[i] = words[i].replace(".", ""), (",", ""), ("/", ""), ("?", ""), ("!", "")

The above code is making words a tuple. And you cannot perform operations like upper() on the tuple. Hence, the error. I think, what you want to do is

words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")

Also, you have declare wordCount = () as tuple. You won't be able to edit wordCount because of this, and this will lead to an error. It should be a dictionary : wordCount = {}

The entire program should look like:

f = open("Les.Miserable.txt", 'r')

words = f.read().split()
words.sort()
wordCount = {}

for i in range(len(words)):
    words[i] = words[i].replace(".", "").replace(",", "").replace("/", "").replace("?", "").replace("!", "")
    words[i] = words[i].upper()
    if words[i] not in wordCount:
        wordCount[words[i]] = 1
    else:
        wordCount[words[i]] += 1
sorted_wordCount = sorted(wordCount.items(), key=operator.itemgetter(1), reverse=True)
print sorted_wordCount

9 Comments

Thank you for answering. I have changed it but it still error on here wordCount[words[i]] = 1 TypeError: 'tuple' object does not support item assignment
What is wordCountint[words[i]]? I think the int is wrongly placed there.
It would be easy for us to help you, if you publish a small snapshot of Les.Miserable.txt
Les miserable strats like this Chapter one Jean Valjean One evening in October 1815, an hour before sunset, a man with a long beard and dusty, torn clothes walked into the town of Digne. He was in his late forties, of medium height, broad-shouldered and strong. A leather cap half-hid his face, which was sunburnt and shining with sweat. His rough yellow shirt was unbuttoned, revealing a hairy chest. On his back was a heavy soldier's bag, and in his hand was a large wooden stick. it is just simple text file
You have declared wordCount = () on top, which is a tuple. Hence, the error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.