Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (he66666):

can someone please help me find the error here? def get_letter_counts(dictionary): '''(dict of {str : list of strs}) -> dict of {str : int} Return a new dictionary with each key a single lowercase letter and each value the number of lowercase words that start with that letter.''' occurrence = {} for (letter, words) in dic.items(): occurrence[letter] = occurrence.get(letter, 0) + 1 occurrence[letter] = int(occurrence[letter]) return occurrence

OpenStudy (he66666):

Btw, in the given dictionary, the key is a single lowercase letter and the value is a list of lowercase word starting with the certain letter.

OpenStudy (he66666):

The function doesn't seem to recognize multiple values so I always end up with only one value and the dictionary itself isn't ordered as well.

OpenStudy (asnaseer):

1) the parameter passed into this function is named 'dictionary', but the loop refers to 'dic' 2) make sure your indentation is correct (a common mistake is to mix tabs and spaces) 3) the code could be simplified by changing the for loop to: for (letter, words) in dic.items(): occurrence[letter] = len(words)

OpenStudy (he66666):

I intentionally changed the parameter to "dictionary" from "dic" to ask this question. I changed the for loop to the one your mentioned but it doesn't work.. and all the indentation is correct as well..

OpenStudy (rsmith6559):

Your second post basically answered your question. You need a loop to iterate through the keys, which you have, but each key is going to give you a list of strings that you'll need another loop to iterate through to get the keys for your new dictionary.

OpenStudy (he66666):

Alright, thanks guys :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!