ITPub博客

首页 > IT职业 > IT生活 > Python list,dict问题解答

Python list,dict问题解答

IT生活 作者:petterchx 时间:2021-09-09 09:21:40 0 删除 编辑

问题:

编写一个函数 most_prolific,其将采用与上述 Beatles_Discography 示例相同的字典格式,并返回发布最多专辑的年份。如果在 Beatles_Discography 中调用该函数,那么应返回 1964 年,这一年相对于其他年份发行的唱片数量较多一些。

from builtins import list
from collections import Counter

Beatles_Discography = {"Please Please Me": 1963, "With the Beatles": 1963,
"A Hard Day's Night": 1964, "Beatles for Sale": 1964, "Twist and Shout": 1964,
"Help": 1965, "Rubber Soul": 1965, "Revolver": 1966,
"Sgt. Pepper's Lonely Hearts Club Band": 1967,
"Magical Mystery Tour": 1967, "The Beatles": 1968,
"Yellow Submarine": 1969 ,'Abbey Road': 1969,
"Let It Be": 1970}

n=[]
for album_title in Beatles_Discography:
##print("title: {}, year: {}".format(album_title, Beatles_Discography[album_title]))
##print(type(n))
n.append(Beatles_Discography[album_title])
print(n)
n1=Counter(n)
print(n1,type(n1))
print(sorted(set(n1),key=lambda x:n1[x])[-1])

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4606/viewspace-2802638/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: php中的设计模式
请登录后发表评论 登录
全部评论

注册时间:2019-10-24

  • 博文量
    131
  • 访问量
    30741