-2
import string

sentence = raw_input("Enter sentence:")

for i in string.punctuation:

    sentence = sentence.replace(i," ")

word_list = sentence.split()
word_list.sort(key=str.lower)
print word_list
for j in word_list:
    print j,":",word_list.count(j)
    word_list.remove(j)

When I use this code and type in a sample sentence, some of my words are not counted correctly:

Sample sentence: I, are.politics:wodng!"frail A P, Python. Python Python frail

output:

['A', 'are', 'frail', 'frail', 'I', 'P', 'politics', 'Python', 'Python', 'Python', 'wodng']

A : 1 frail : 2 I : 1 politics : 1 Python : 3 wodng : 1

What happened to the words "are" and "P"? I know the problem is happening in the last few lines but I don't know what's causing it.

Thanks!

Clarence
  • 21
  • 1
  • 4
  • 2
    I posted any answer, but this fix requires rethinking your algorithm a bit. I deleted my answer since I'm on my phone and am unable to write a working solution right now. My original answer: "You're removing from a list while iterating over it, don't do that. That often leads to undesirable behavior. Just remove the call to `word_list.remove`." You'll need to find a different way to process the list. Instead of using `count`, it'll probably be easier to just add the words into a dictionary that has the count as its values. – Carcigenicate Aug 30 '17 at 15:42
  • How should I process the list then? I just want a count of how many of each word there is in the list. – Clarence Aug 30 '17 at 16:08
  • The/A typical way is to use a dictionary. Try to add a word to the dictionary. If it's already in the dictionary, add one to its value. If it's not in the dictionary, add it will a value of 0. Do you know how to use a dictionary? – Carcigenicate Aug 30 '17 at 16:10
  • https://stackoverflow.com/questions/20510768/python-count-frequency-of-words-in-a-list – Carcigenicate Aug 30 '17 at 16:10
  • 1
    Possible duplicate of [How to remove list elements in a for loop in Python?](https://stackoverflow.com/questions/10665591/how-to-remove-list-elements-in-a-for-loop-in-python) – Carcigenicate Aug 30 '17 at 16:12
  • I'm not sure how to do that. What would it look like? Thanks for the help. – Clarence Aug 30 '17 at 16:13
  • I'm about to start work, so I can't write an answer. Read the first link I posted to see some solutions. – Carcigenicate Aug 30 '17 at 16:14

2 Answers2

0

At the end of your script, your list is not empty

debut algo

You remove each time a value, so the pointer jumps one value each time

Xire
  • 166
  • 7
  • Do you have any suggestions as to what I should do about this? – Clarence Aug 30 '17 at 16:09
  • Create an hash list, and add each word you see. Add in you loop check if the word already exist in your hash list – Xire Aug 30 '17 at 16:17
  • I simply need the count of each word, in that exact format (i.e. word:number). I used the removing function because if not, if the word appeared more than once, it would be counted more than once as well, like so: Python: 3 Python: 3 Python: 3 All the suggestions involving using Counter I can't use because I've never been taught those and this is supposed to be an assignment. Could you perhaps teach me how to use this hash list method of yours, or alternatively, suggest any way I could obtain the word frequency without duplication? – Clarence Aug 30 '17 at 16:19
0

The problem in your code is, that you remove elements from the list over which you are iterating.

Therefore I suggest to separate the iterator by converting the word_list into a set. Then you can iterate over the set word_iter, which contains every word just one time. Then you also don't need to remove anything anymore. Disadvantage is the unordered result, as sets are unordered. But you can put the result in a list and order that afterwards:

import string

sentence = raw_input("Enter sentence:")

for i in string.punctuation:
    sentence = sentence.replace(i," ")

word_list = sentence.split()
word_list.sort(key=str.lower)
print word_list
result = []
word_iter = set(word_list)
for j in word_iter:
    print j, ':', word_list.count(j)
    result.append( (j, word_list.count(j)) )

result:

A : 1
wodng : 1
Python : 3
I : 1
P : 1
are : 1
frail : 2
politics : 1
jps
  • 20,041
  • 15
  • 75
  • 79