获取python字典中字母的百分比

获取python字典中字母的百分比,python,dictionary,Python,Dictionary,在给定的字典中,每个键都是一个小写字母,每个值都是以该字母开头的小写单词数。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 loo

在给定的字典中,每个键都是一个小写字母,每个值都是以该字母开头的小写单词数。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) #??
这就是我被卡住的地方。我知道我需要创建一个值的和,以便进行浮点除法

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) #??

如果每个值是每个字母的字数,那么求和的代码是:

sum = 0
for (key, value) in dictionary:
    sum = sum + value
那么这是一个:

def get_letter_percentage(dictionary, letter):
    return dictionary[letter] / sum

如果每个值是每个字母的字数,那么求和的代码是:

sum = 0
for (key, value) in dictionary:
    sum = sum + value
那么这是一个:

def get_letter_percentage(dictionary, letter):
    return dictionary[letter] / sum

也许你可以在你的函数中尝试这样的东西:

dictionary = {'a': 5, 'b': 8, 'c':15}
sum = 0
for (key, value) in dictionary.items(): sum += value
percentage = dictionary['a'] / (sum + 0.0)
print "percentage of '%s' is %.2f %%" % ('a' , percentage*100)

也许你可以在你的函数中尝试这样的东西:

dictionary = {'a': 5, 'b': 8, 'c':15}
sum = 0
for (key, value) in dictionary.items(): sum += value
percentage = dictionary['a'] / (sum + 0.0)
print "percentage of '%s' is %.2f %%" % ('a' , percentage*100)
百分比是事件数除以总事件数。 注意乘法1.0以避免整数除法

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) #??
百分比是事件数除以总事件数。 注意乘法1.0以避免整数除法。

我想出了以下方法:

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) #??
mydict = {"a":5, "b":1, "c":4, "d":3, "e":6}


def get_letter_percentage(dictionary, s):
    sum = 0
    for key in dictionary:
        sum += dictionary[key]

    return float(dictionary[s]) / float(sum)

print get_letter_percentage(mydict, "b")
我想到了这个:

mydict = {"a":5, "b":1, "c":4, "d":3, "e":6}


def get_letter_percentage(dictionary, s):
    sum = 0
    for key in dictionary:
        sum += dictionary[key]

    return float(dictionary[s]) / float(sum)

print get_letter_percentage(mydict, "b")

刚刚意识到我需要返回一个float,所以我将去掉print语句刚刚意识到我需要返回一个float,所以我将去掉print语句