蟒蛇新手。這是一個嵌套字典,有兩本書,每本書有 8 個屬性。
book_collection ={17104: {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}, 37115: {'Title': 'Aim High', 'Author': 'George Tayloe Winston', 'Publisher': 'Manning Hall Press', 'Pages': '663', 'Year': '2014', 'Copies': 5, 'Available': 5, 'ID': 37115}}
for id, book in book_collection.items():
for book_attribute, attribute_value in book.items():
print(book_attribute, ': ', attribute_value, sep='')
輸出:
Title: A River
Author: Elisha Mitchell
Publisher: FPG Publishing
Pages: 345
Year: 2014
Copies: 2
Available: 2
ID: 17104
Title: Aim High
Author: George Tayloe Winston
Publisher: Manning Hall Press
Pages: 663
Year: 2014
Copies: 5
Available: 5
ID: 37115
如何在每本書之間添加一個空格,并將“ID”屬性帶到每本書的第一行。輸出應該如下所示:
ID: 17104
Title: A River
Author: Elisha Mitchell
Publisher: FPG Publishing
Pages: 345
Year: 2014
Copies: 2
Available: 2
ID: 37115
Title: Aim High
Author: George Tayloe Winston
Publisher: Manning Hall Press
Pages: 663
Year: 2014
Copies: 5
Available: 5
如果有 20 本書,我如何只列印前 10 本書并請求用戶允許繼續?
uj5u.com熱心網友回復:
用這個:
for id, book in book_collection.items():
for book_attribute, attribute_value in book.items():
print(book_attribute, ': ', attribute_value, sep='')
print()
您可以只使用 index() 函式來檢查索引是否為 9 然后這樣問
for id, book in book_collection.items():
if book_collection.index(id) == 9:
n = int(input("Press 0 to continue or else to exit"))
if n != 0:
break
for book_attribute, attribute_value in book.items():
print(book_attribute, ': ', attribute_value, sep='')
print()
uj5u.com熱心網友回復:
字典的 items 方法方法回傳一個可迭代的元組(不可變串列)。產生的每個元組代表一對鍵和值,鍵在元組的 0 索引中,值在 1 索引中。
您正在使用的 for 回圈 - for book_attribute, attribute_value in book.items():- 是“獲取元組中的兩個值并將它們分配給這些變數,然后運行此塊中的代碼”的語法糖。
這樣想可能更容易:
>>> book_dict = {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}
>>> book_dict_entries = list(book_dict.items())
>>> print(book_dict_entries)
[('Title', 'A River'), ('Author', 'Elisha Mitchell'), ('Publisher', 'FPG Publishing'), ('Pages', '345'), ('Year', '2014'), ('Copies', 2), ('Available', 2), ('ID', 17104)]
從這里有幾個方向可以去。一種方法是——因為它只是一個串列——您可以搜索表示 ID 欄位的那個,并將其與該串列中的第一個元素交換。或者,在將其轉換為串列之前,只需從字典中列印 ID,然后在列舉其余欄位時過濾該欄位。
至于你的第二個問題,如果你想在某個點列印一個空行 - 只需print不帶引數呼叫。就像你列印完每本字典一樣。
uj5u.com熱心網友回復:
我會將帶有 ID 的 dict 定義為第一個鍵,因為從 Python 3.7 開始(不是之前!) dicts 是有序的
將 ID 作為第一個鍵,并print()在每個內部回圈之后添加。
book_collection = {
17104:
{'ID': 17104, 'Title': 'A River', 'Author': 'Elisha Mitchell',
'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014',
'Copies': 2, 'Available': 2, },
37115:
{'ID': 37115, 'Title': 'Aim High', 'Author': 'George Tayloe Winston',
'Publisher': 'Manning Hall Press', 'Pages': '663', 'Year': '2014',
'Copies': 5, 'Available': 5}
}
for id, book in book_collection.items():
for book_attribute, attribute_value in book.items():
print(book_attribute, ': ', attribute_value, sep='')
print()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528185.html
標籤:Python字典
下一篇:查找總和為6的字典鍵
