Python list,dict問題解答

來源:互聯網
上載者:User

標籤:type   yellow   please   gic   樣本   hearts   out   mos   pen   

問題:

編寫一個函數 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])

Python list,dict問題解答

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.