在给定的字典中,每个键都是一个小写字母,每个值都是以该字母开头的小写单词的数量。str参数是一个小写字母。根据字典中的值,返回以该字母开头的单词的百分比。
注意:使用浮点除法。
def get_letter_percentage(dictionary, s):
'''(dict of {str : int}, str) -> float'''
# Start with a counter for the sum of the values.
count = 0
# And then look at the key and value of the dictionary
for (key, value) in dictionary.items(): 这就是我被卡住的地方。我知道我需要创建一个值的总和,以便进行浮点除法。
# guessing it is something along these lines
count = len(values) #??发布于 2012-03-27 23:32:19
如果每个值都是每个字母的字数,那么求和的代码应该是:
sum = 0
for (key, value) in dictionary:
sum = sum + value那么这就是一个例子:
def get_letter_percentage(dictionary, letter):
return dictionary[letter] / sumhttps://stackoverflow.com/questions/9892658
复制相似问题