看着我的代碼...我可以一次檢查字典中的所有值嗎?
# 1 = in the bookshelf, 0 = not in the bookshelf
bookshelf = {}
bookshelf["The Incredible Book About Pillows"] = 1
bookshelf["Little Fox and his Friends"] = 1
bookshelf["How To Become a Superhero: Part 1"] = 0
if bookshelf["The Incredible Book About Pillows"] == 1:
print("The Incredible Book about Pillows")
if bookshelf["Little Fox and his Friends"] == 1:
print("Little Fox and his Friends")
if bookshelf["How To Become a Superhero: Part 1"] == 1:
print("How To Become a Superhero: Part 1")
...有什麼更好的方法來打印所有當前在書架書(有值1)?或者,當我將它們從書架中取出並在放回書架時添加它們時,簡單地將它們從字典中刪除是最簡單的方法嗎?
我是小白,所以我真的不明白這一切...你能更徹底地解釋一下? – PhP
解釋(和你想去的方向)很大程度上取決於你想用你的字典做什麼。你只是保留你擁有的書籍清單嗎?如果是這樣的話,那麼列表可能會比字典更好。 –