第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何訪問(wèn)另一個(gè)集合內(nèi)的集合的值

如何訪問(wèn)另一個(gè)集合內(nèi)的集合的值

慕桂英4014372 2023-06-27 17:26:40
所以我有一個(gè)集合位于另一個(gè)集合內(nèi),如下所示:country = {        'spain': { 'capital':'madrid', 'population':46.77 },       'france': { 'capital':'paris', 'population':66.03 },       'germany': { 'capital':'berlin', 'population':80.62 },       'norway': { 'capital':'oslo', 'population':5.084 },       'peru' : {'capital':'lima', 'population':250}     }我需要顯示每個(gè)國(guó)家/地區(qū)每個(gè)首都的名稱(chēng),因此我決定使用 for 循環(huán)。for countries in country:    print('capital city of '+countries+' is '+countries)現(xiàn)在我可以訪問(wèn)每個(gè)國(guó)家,但不能訪問(wèn)首都。我嘗試在 for 循環(huán)之間添加一些變量,因?yàn)樗o了我一個(gè)“太多無(wú)法打包錯(cuò)誤”。下面是我的第二次嘗試。for countries,value in country.items():    print('capital city of '+countries+' is '+countries[value])無(wú)論如何,我可以超越這個(gè)嗎?
查看完整描述

2 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

順便說(shuō)一句,你已經(jīng)嵌套了dicts。你沒(méi)有任何sets。


您的意思是這樣嗎:


for name,value in country.items():

    print(f'capital city of {name} is {value["capital"]}')

輸出:


capital city of spain is madrid

capital city of france is paris

capital city of germany is berlin

capital city of norway is oslo

capital city of peru is lima

更新:我在語(yǔ)句中使用了 an f-stringin print(),這有時(shí)是一種更簡(jiǎn)單的格式化字符串的方法。


查看完整回答
反對(duì) 回復(fù) 2023-06-27
?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

迭代字典country并使用值打印適當(dāng)?shù)拇髮?xiě)字母key:


country = { 

       'spain': { 'capital':'madrid', 'population':46.77 },

       'france': { 'capital':'paris', 'population':66.03 },

       'germany': { 'capital':'berlin', 'population':80.62 },

       'norway': { 'capital':'oslo', 'population':5.084 },

       'peru' : {'capital':'lima', 'population':250}

     }

     


for country, capitals in country.items():

    print("Country: {0}, Capital: {1}".format(country, capitals['capital']))

輸出:


Country: peru, Capital: lima                                                                                                                                                 

Country: germany, Capital: berlin                                                                                                                                            

Country: spain, Capital: madrid                                                                                                                                              

Country: france, Capital: paris                                                                                                                                              

Country: norway, Capital: oslo

編輯:


單行:


print([{"Country: {0}, Capital: {1}".format(cn, cp['capital'])} for cn, cp in country.items()])

輸出:


[{'Country: spain, Capital: madrid'}, {'Country: germany, Capital: berlin'}, {'Country: norway, Capital: oslo'}, {'Country: france, Capital: paris'}, {'Country: peru, Capita

l: lima'}]


查看完整回答
反對(duì) 回復(fù) 2023-06-27
  • 2 回答
  • 0 關(guān)注
  • 173 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)