在python的库中,我们现在有两个字典的Python实现,它们dict在本机dict类型之上进行子类化.
Python的拥护者总是倾向于在可能的情况下defaultdict过度使用dict.setdefault.甚至医生引用了这一点This technique is simpler and faster than an equivalent technique using dict.setdefault():
以类似的方式,由于字典不维护顺序,因此在替代使用时,优选使用OrderedDict过度使用dict然后对项目进行排序.
在上述两种情况下,代码肯定更清晰,但代价是性能损失.
在回答和评论基于项目的问题python唯一列表之一时,我偶然发现dict使用defaultdict和时本机的性能损失OrderedDict.对于性能优势dict解决方案而言,数据的大小似乎也并非无关紧要.
我相信There should be one-- and preferably only one --obvious way to do it.,那么首选方式是什么?