作者teddyissad (了不起 負責)
看板Python
標題[問題]
時間Mon Jul 22 17:41:54 2019
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://webptt.com/m.aspx?n=bbs/Python/M.1563788516.A.4C3.html
※ 編輯: teddyissad (180.217.208.184 臺灣), 07/22/2019 17:42:24
1F:→ stucode: dict_counter = {} 是建立空字典不是空集合。 07/22 18:07
2F:→ stucode: dict_counter[food] 是查詢字典中 key 為 food 變數內容 07/22 18:08
3F:→ stucode: 的對應值為何,假設 07/22 18:08
4F:→ stucode: dict_counter = {'spam': 3, 'egg': 1} 07/22 18:08
5F:→ stucode: food = 'spam' 07/22 18:08
6F:→ stucode: 那 dict_counter[food] 就是 3 07/22 18:08
7F:→ stucode: 而 dict_counter[food] += 1 是把該對應值 +1, 07/22 18:08
8F:→ stucode: 變成 {'spam': 4, 'egg': 1} 07/22 18:09
9F:推 TitanEric: 利用dict作counting 07/22 18:54
10F:→ TitanEric: 個人覺得not in dict比較直覺 也容易閱讀 07/22 18:54
11F:→ yummy8765: =0是指定 +=1是運算 我會這樣理解 07/22 22:42