我需要在任何一天找到人數最多的月份。我寫的代碼只是看起來的天量最多的,而不是有多少人在那一天Python嵌套字典
def total_days(birthdays):
'''
>>> total_days({"Jan": {2: ["Ben"], 3: ["Sarah"], 6: ["Rob"]},
"Feb": {1: ["Jimmy", "Timmmy"], 30: ["Sam", "Tony"]}})
'Feb'
'''
total = 0
month = ''
for i in birthdays:
new = len(birthdays[i])
if new > total:
total = new
month = i
return month
此代碼返回揚因爲在一月份有3個日期,而二月有2個生日,但我需要一個月與這應該是二月
感謝
一個簡單的解決辦法是使用嵌套的'for'循環。 – spicypumpkin
我想,但我不知道如何使它正常工作 – Patrick