python 字典使用注意:访问dict中没有的对应key的项目

dict.get(key, default=None)
      key -- This is the Key to be searched in the dictionary.
     default -- This is the Value to be returned in case key does not exist. 

window_counts = {}
window_counts[char] = window_counts.get(char, 0) + 1  
## 如果字典window_counts中没有key==char的项,
## 则get()函数返回0,此时将在字典window_counts创建一个新的项。


 

发布了10 篇原创文章 · 获赞 2 · 访问量 7728

猜你喜欢

转载自blog.csdn.net/ftfy123/article/details/104559960