当前位置:网站首页>Easy to use dictionary -defaultdict
Easy to use dictionary -defaultdict
2022-06-25 02:54:00 【Upward a Peng】
Reference link :https://blog.csdn.net/sGDUTBMW/article/details/108962367
defaultdict Class is like a dict, But it is initialized with a type ;
defaultdict Class takes a type as an argument , When the accessed key does not exist , You can instantiate a value as the default ; If NULL .
Compared with dict, When the accessed key does not exist , No mistake. , Only one default value will be returned , besides , In the use of dict In the process of , identical key Only one... Can be saved value, When there is a repetition key when , Will be covered value, Not conducive to finding the same key Multiple value The situation of , and defaultdict You can return multiple value value .
Example :
Output :
defaultdict In addition to accepting the type name as an argument to the initialization function , You can also use any callable function without parameters , At that time, the return result of this function will be taken as the default value , This makes the default value more flexible
from collections import defaultdict
strings = ('puppy', 'kitten', 'puppy', 'puppy',
'weasel', 'puppy', 'kitten', 'puppy')
def zero():
return 0
connts = defaultdict(zero) # Use custom without parameters zero function
counts = defaultdict(lambda: 0) # Use lambda To define simple functions , The effect same as above
for s in strings:
counts[s] += 1
print(counts)
Output :
边栏推荐
- Expressing the transformation of two coordinate systems with vectors
- How transformers Roberta adds tokens
- Software testing salary in first tier cities - are you dragging your feet
- 李宏毅《机器学习》丨6. Convolutional Neural Network(卷积神经网络)
- 怎么开户打新债 开户是安全的吗
- Distributed transaction solutions and code implementation
- Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (1) -- migrate data to node 1
- Array - fast and slow pointer in one breath
- It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
- Software testing weekly (issue 77): giving up once will breed the habit of giving up, and the problems that could have been solved will become insoluble.
猜你喜欢
Difference between left join on and join on
Random list random generation of non repeating numbers
UnityShader入门精要——PBS基于物理的渲染
Solution of separating matlab main window and editor window into two interfaces
AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了
Distributed transaction solutions and code implementation
MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
DSPACE的性能渲染问题
automated testing
用向量表示两个坐标系的变换
随机推荐
目录权限错误导致 Oracle 11g rac 集群数据库无法启动的问题
vie的刷新机制
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
Post competition summary of kaggle patent matching competition
Lihongyi, machine learning 6 Convolutional neural network
Charles 抓包工具
VSCode中如何实现点击DOM自动定位到相应代码行
left join on和 join on的区别
高速缓存Cache详解(西电考研向)
分布式事务解决方案和代码落地
UnityShader入门精要——PBS基于物理的渲染
E - average and median
高数 | 精通中值定理 解题套路汇总
Call system function security scheme
ACL access control of squid proxy server
Go synchronization waiting group
PE file infrastructure sorting
如何卸载cuda
internship:svn的使用
Unity存档系统——Json格式的文件