看板 Python 關於我們 聯絡資訊
https://i.imgur.com/KrQzV0M.jpg
不好意思,想請問一下,我把上述中間那行程式碼的原因理解為 先將dict_counter令為空集合,當作初始條件, 然後food將集合中的[spam spam egg spam ]用for依次拿來使用。 我想請問其中的dict_counter[food]是什麼意思 另外dict_counter[food]+=1 是在表達什麼 ? 謝謝 100p酬謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.217.208.184 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1563788516.A.4C3.html ※ 編輯: teddyissad (180.217.208.184 臺灣), 07/22/2019 17:42:24
stucode: dict_counter = {} 是建立空字典不是空集合。 07/22 18:07
stucode: dict_counter[food] 是查詢字典中 key 為 food 變數內容 07/22 18:08
stucode: 的對應值為何,假設 07/22 18:08
stucode: dict_counter = {'spam': 3, 'egg': 1} 07/22 18:08
stucode: food = 'spam' 07/22 18:08
stucode: 那 dict_counter[food] 就是 3 07/22 18:08
stucode: 而 dict_counter[food] += 1 是把該對應值 +1, 07/22 18:08
stucode: 變成 {'spam': 4, 'egg': 1} 07/22 18:09
TitanEric: 利用dict作counting 07/22 18:54
TitanEric: 個人覺得not in dict比較直覺 也容易閱讀 07/22 18:54
yummy8765: =0是指定 +=1是運算 我會這樣理解 07/22 22:42