簡體   English   中英

卡在 PYTHON 編程的這個問題上

[英]STUCK ON THIS QUESTION FOR PYTHON PROGRAMMING

umSchools 詞典將密歇根州的學校名稱與它們的成立年份對應起來。 編寫代碼,創建一個名為 oldSchools 的字典,該字典具有來自 umSchools 的鍵/值對,對應於下面列表學校中列出的學校。

執行以下操作: 遍歷學校:使用當前學校作為鍵和 umSchools 中的相應值創建一個新字典 Print oldSchools

umSchools = { "A. Alfred Taubman College of Architecture & Urban Planning": 1906, "College of Engineering": 1854, "College of Literature, Science, and the Arts": 1841, "Gerald R. Ford School of Public Policy": 1914, "Horace H. Rackham School of Graduate Studies": 1912, "Penny W. Stamps School of Art & Design": 1974, "School of Dentistry": 1875, "School of Education": 1921, "School of Information": 1996, "School of Kinesiology": 1984, "School of Law": 1859, "School of Medicine": 1850, "School of Music, Theatre & Dance": 1880, "School of Natural Resources & Environment": 1927, "School of Nursing": 1893, "School of Pharmacy": 1876, "School of Public Health": 1941, "School of Social Work": 1951, "Stephen M. Ross School of Business": 1924 }

schools= [ "College of Engineering","School of Nursing","School of Dentistry"]

WHAT I CURRRENTLY HAVE

oldSchools= dict.fromkeys(schools, d)
print(oldSchools)

I AM HONESTLY LOST

NEEDED OUTPUT:
{'College of Engineering': 1854, 'School of Nursing': 1893, 'School of Dentistry': 1875}

它很容易:

umSchools = { "A. Alfred Taubman College of Architecture & Urban Planning": 1906, "College of Engineering": 1854, "College of Literature, Science, and the Arts": 1841, "Gerald R. Ford School of Public Policy": 1914, "Horace H. Rackham School of Graduate Studies": 1912, "Penny W. Stamps School of Art & Design": 1974, "School of Dentistry": 1875, "School of Education": 1921, "School of Information": 1996, "School of Kinesiology": 1984, "School of Law": 1859, "School of Medicine": 1850, "School of Music, Theatre & Dance": 1880, "School of Natural Resources & Environment": 1927, "School of Nursing": 1893, "School of Pharmacy": 1876, "School of Public Health": 1941, "School of Social Work": 1951, "Stephen M. Ross School of Business": 1924 }
schools= [ "College of Engineering","School of Nursing","School of Dentistry"]

oldSchools = {}

#cycle through schools to find the values
for i in schools:
    if i in umSchools.keys():
        oldSchools[i]=umSchools[i] # adding values to oldSchools

print(oldSchools)

或者在一行中:

oldSchools = {a:b for a, b in umSchools.items() if b in schools}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM