作者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/cn.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