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

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

Python字典輸出問題

Python字典輸出問題

Cats萌萌 2023-10-06 18:53:04
我是 python 和這個(gè)論壇的新手。在線學(xué)習(xí)對(duì)我來說不起作用,所以我不能只去找導(dǎo)師。這可能是我忘記的一些小事。我歡迎您能給我任何幫助。我試圖使輸出看起來像這樣: Her name is Emmylou; 她有望于 2021 年秋季畢業(yè);她的賬單已付清;她的專業(yè)是考古學(xué);她屬于這些學(xué)校俱樂部——攝影、表演和歡樂合唱團(tuán)Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}for Key, Value in Emm.items():print(f"Her {Key} is {Value} and she is on track to {Key} in {Value}; Her {Key} is {Value}; Her {Key} is {Value}; She belongs to these school {Key} {Value}")輸出很混亂,當(dāng)我運(yùn)行它時(shí)看起來像這樣:Her name is Emmylou and she is on track to name in Emmylou; Her name is Emmylou; Her name is Emmylou; She belongs to these school name EmmylouHer graduate is Fall 2021 and she is on track to graduate in Fall 2021; Her graduate is Fall 2021; Her graduate is Fall 2021; She belongs to these school graduate Fall 2021Her bill is paid and she is on track to bill in paid; Her bill is paid; Her bill is paid; She belongs to these school bill paidHer major is Archeology and she is on track to major in Archeology; Her major is Archeology; Her major is Archeology; She belongs to these school major ArcheologyHer clubs- is Photography, Acting and Glee and she is on track to clubs- in Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; She belongs to these school clubs- Photography, Acting and Glee
查看完整描述

3 回答

?
蝴蝶不菲

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

正如其他人告訴你的那樣,你正在迭代字典,并且在每次迭代中,鍵和值都會(huì)被替換并打印在新行中。


如果你想使用字典在一行中打印,你可以嘗試將字典轉(zhuǎn)換為數(shù)組并使用 format 方法打印。


Emm = {

    'name' : 'Emmylou',

    'graduate' : 'Fall 2021',

    'bill' : 'paid',

    'major' : 'Archeology',

    'clubs-' : 'Photography, Acting and Glee'

}


items = []

for (key, value) in Emm.items():

    items = items + [key, value]

print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*items))



查看完整回答
反對(duì) 回復(fù) 2023-10-06
?
慕的地8271018

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

首先,我假設(shè)您實(shí)際上已經(jīng)在代碼中縮進(jìn)了 print 語句,否則它根本無法工作。


問題是,對(duì)于每個(gè)循環(huán),您都在所有位置填寫相同的鍵/值對(duì)。


根據(jù)目的,您可以通過執(zhí)行以下操作來獲得聲明:


Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}

print(f"Her name is {Emm['name']} and she is on track to graduate in {Emm['graduate']}; Her major is {Emm['major']}; Her clubs - is {Emm['clubs-']}")

迭代字典時(shí)可能面臨的另一個(gè)問題是,除非使用 python 3.7 或更高版本,否則無法保證項(xiàng)目在字典中保存的順序。因此,您的鍵/值對(duì)可能不會(huì)按照它們進(jìn)入的順序出現(xiàn)。


查看完整回答
反對(duì) 回復(fù) 2023-10-06
?
手掌心

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

在您的代碼中,您將迭代數(shù)據(jù)中的每個(gè)鍵值對(duì);因此,您最終打印了 5 次,每次都使用一個(gè)鍵值對(duì),而不是打印 1 次,每次都使用所有鍵值對(duì)。


嘗試這個(gè)。


Emm = [

    ('name', 'Emmylou'),

    ('graduate', 'Fall 2021'),

    ('bill', 'paid'),

    ('major', 'Archeology'),

    ('clubs-', 'Photography, Acting and Glee'),

]


flat_items = [item for pair in Emm for item in pair]

print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*flat_items))



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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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