Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Carl Hicks
Java Web Development Techdegree Student 3,453 PointsStuff on python word count ex, need a little help.
Having a little trouble with a this for loop.
def word_count():
return output
test_string = "I am that I am"
list_of_string = test_string.split(' ')
output = {}
i = 0
print(list_of_string)
for list_of_string[i] in list_of_string:
try:
test = list_of_string.count(list_of_string[i])
output.update({list_of_string[i]: test})
item_to_remove = list_of_string[i]
while item_to_remove in list_of_string:
list_of_string.remove(item_to_remove)
i += 1
except:
print("This is doing somthing you didn't except")
print(list_of_string)
print(output)
# print(word_count("I am that I am"))
returns:
['I', 'am', 'that', 'I', 'am']
['am', 'am']
{'that': 1, 'I': 2}
Still trying to work though the problem haven't added it to the function. (I'm about 80% there but seem to be missing something.)
1 Answer
Cody Te Awa
8,820 PointsHi Carl, The problem here I think is that you are seem to be getting confused with the exact functionality of the for loop. I see you are indexing the list in the for statement variable. Now although this is syntactically okay, and it works.. You may as well be using a while loop. Might just want to quickly revise the for loop. But for now the problem is you are removing elements from the list you are trying to iterate through. So at the third loop the for loop thinks its at its 3rd iteration but at the point theres only 2 elements in the list those 2 elements being ['am', 'am'], which is when the loop stops.
With that being said in terms of having your code work correctly you can try:
test_string = "I am that I am"
list_of_string = test_string.split(' ')
output = {}
i = 0
print(list_of_string)
# Create a copy to remove words from instead of altering the original list
stringListCopy = list_of_string.copy()
# Take a set of the original list to only take the counts of the word once
list_of_string = list(set(list_of_string))
for list_of_string[i] in list_of_string:
try:
test = list_of_string.count(list_of_string[i])
output.update({list_of_string[i]: test})
item_to_remove = list_of_string[i]
while item_to_remove in stringListCopy:
stringListCopy.remove(item_to_remove)
#i += 1 remove this line the for loop iterates, no need for manual iteration
except:
print("This is doing somthing you didn't except")
Alternatively, You can try this (just an option):
test_string = "I am that I am"
list_of_string = test_string.split(' ')
output = {}
for word in list_of_string:
if word in output:
output[word] += 1 #if word in the output increments its count
else:
output[word] = 1 # otherwise add it to the output with a count of 1
-Hope this helps!! =)
Carl Hicks
Java Web Development Techdegree Student 3,453 PointsCarl Hicks
Java Web Development Techdegree Student 3,453 PointsI actually figured out a more idiomatic way to handle this before posting but was a little puzzled by the result, I was getting. Someone explained the issue on the Python slack channel for me but I thank you and your answer is spot on.