亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python字典輸出問題

Python字典輸出問題

Cats萌萌 2023-10-06 18:53:04
我是 python 和這個論壇的新手。在線學習對我來說不起作用,所以我不能只去找導師。這可能是我忘記的一些小事。我歡迎您能給我任何幫助。我試圖使輸出看起來像這樣: Her name is Emmylou; 她有望于 2021 年秋季畢業;她的賬單已付清;她的專業是考古學;她屬于這些學校俱樂部——攝影、表演和歡樂合唱團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}")輸出很混亂,當我運行它時看起來像這樣: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貢獻1810條經驗 獲得超4個贊

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


如果你想使用字典在一行中打印,你可以嘗試將字典轉換為數組并使用 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))



查看完整回答
反對 回復 2023-10-06
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

首先,我假設您實際上已經在代碼中縮進了 print 語句,否則它根本無法工作。


問題是,對于每個循環,您都在所有位置填寫相同的鍵/值對。


根據目的,您可以通過執行以下操作來獲得聲明:


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-']}")

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


查看完整回答
反對 回復 2023-10-06
?
手掌心

TA貢獻1942條經驗 獲得超3個贊

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


嘗試這個。


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))



查看完整回答
反對 回復 2023-10-06
  • 3 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號