Python list,dict問題解答
阿新 • • 發佈:2018-01-27
cal sub pri 回發 調用 port 一個 uil har 問題:
"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} ##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])
編寫一個函數 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,
"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(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])
Python list,dict問題解答