我不明白为什么同样的方法适用于一个函数和字典而它不适用于第二个,即使我只是“复制”它。
allGuests = {"Alice": {"apples": 5, "pretzels": 12}, "Bob": {"ham sandwiches": 3, "apples": 2}, "Carol": {"cups": 3, "apple pies": 1}}
allCalories = {"apples": {"Calories": 100, "fat": 10}, "pretzels": {"Calories": 200, "fat": 20}, "ham sandwiches": {"Calories": 300, "fat": 30}, "cups": {"Calories": 0, "fat": 0}, "apple pies": {"Calories": 500, "fat": 50}}
def totalBrought(guests, item):
numBrought = 0
for k, v in guests.items():
numBrought = numBrought + v.get(item, 0)
return numBrought
def totalCalories(dish, calories):
cal = 0
for k, v in dish.items():
cal = cal + v.get(calories, 0)
return cal
print(" Number of Things being brought: ")
print(" - Apples " + str(totalBrought(allGuests, "apples")))
print(" Calories of apples " + str(totalCalories(allCalories, "apples")))
print(" - Cups " + str(totalBrought(allGuests, "cups")))
print(" - Cakes " + str(totalBrought(allGuests, "cakes")))
print(" - Ham Sandwiches " + str(totalBrought(allGuests, "ham sandwiches")))
print(" - Apple Pies " + str(totalBrought(allGuests, "apple pies")))
(错误的)结果是:
Number of Things being brought:
- Apples 7
Calories of apples 0
- Cups 3
- Cakes 0
- Ham Sandwiches 3
- Apple Pies 1
任何人都可以向我解释为什么它不起作用?看似蟒蛇初学者的简单逻辑差距.... 提前谢谢!
答案 0 :(得分:6)
错误在于:
def totalCalories(dish, calories):
cal = 0
for k, v in dish.items():
cal = cal + v.get(calories, 0)
return cal
您可以依次循环遍历每个项目,例如{"Calories": 100, "fat": 10}
,{"Calories": 200, "fat": 20}
等等。但是您要查询不存在的'apples'
,并继续添加0。
你已经拿到了钥匙!您无需再次遍历dict
。这违背了它的目的。
您可以这样做:
def totalCalories(dish, calories):
return dish[calories]["Calories"]
我相信你错误地命名了你的变量,这让你感到困惑。试试这个:
def totalCalories(calorie_dict, item):
return calorie_dict[item]["Calories"]
答案 1 :(得分:1)
这似乎是使用Counter
模块中的Collections
对象的一个很好的用例。它允许您在一次通话中计算各种食物的所有频率,而不是每个食物项目需要单独的功能调用。
# Python 2.7 Syntax
from collections import Counter
allGuests = {"Alice": {"apples": 5, "pretzels": 12}, "Bob": {"ham sandwiches": 3, "apples": 2}, "Carol": {"cups": 3, "apple pies": 1}}
def totalBrought(guests):
food_counter = Counter()
for k,v in guests.iteritems():
food_counter.update(v)
return food_counter
print totalBrought(allGuests)
>> Counter({'pretzels': 12, 'apples': 7, 'cups': 3, 'ham sandwiches': 3, 'apple pies': 1})
答案 2 :(得分:0)
感谢您的帮助。为了更好地理解,我改变了命名(见下文)。 可能有更复杂的解决方案,但我喜欢坚持“初始”方法,因为我还是初学者,不想跳过主题,只是调整更复杂的方法。
allCalories = {"apples": {"Calories apples": 80, "Fat apples": 2}, "pretzels":
{"Calories pretzels": 200, "Fat pretzels": 20},"ham sandwiches":
{"Calories ham sandwiches": 300, "Fat ham sandwiches":30}"cups":
{"Calories cups": 0, "Fat Cups": 0},"apple pies":
{"Calories apple pies": 500, "Fat apple pies": 50}}
然后例如:
print(" -> Total Calories of Apples " +
str(totalCalories(allCalories,"Calories apples")*
(totalBrought(allGuests,"apples"))))
我还添加了一些其他行,所以结果现在看起来像这样:
Number, Calories and Fat of Things being brought:
- Apples 7
-> Total Calories of Apples 560
-> Total Fat of Apples 14 Gramm
- Pretzels 12
-> Total Calories of Pretzels 2400
-> Total Fat of Pretzels 240 Gramm
- Cups 3
-> Total Calories of Cups 0
-> Total Fat of Cups 0 Gramm
- Cakes 0
-> Total Calories of Cakes 0
-> Total Fat of Cakes 0 Gramm
- Ham Sandwiches 3
-> Total Calories of Ham Sandwiches 900
-> Total Fat of Ham Sandwiches 90 Gramm
- Apple Pies 3
-> Total Calories of Apple Pies 1500
-> Total Fat of Apple Pies 150 Gramm
==>> Total Calories of Things being brought: 5360
==>> Total Fat of Things being brought: 494