使用 defaultdict 替换 python 中的 try 和/或 if 语句 [英] Using defaultdict to replace try and/or if statements in python

查看:39
本文介绍了使用 defaultdict 替换 python 中的 try 和/或 if 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现并开始使用默认词典来替换几个更庞大的结构.我在《蟒蛇之禅》中读到,蟒蛇的关键点之一是应该有一种——最好只有一种——明显的方法来做到这一点."

I have recently found and started using default dictionaries to replace several more bulky constructs. I have read in 'the zen of python' that one of the key points of python is "There should be one-- and preferably only one --obvious way to do it."

基于该标准(或者更实际地基于内存使用或速度),以下哪个(或完全不同的东西)是最好的?我有一种预感,第一个是正确的,但希望得到其他人的意见.

Based on that criteria (or perhaps more practically based on memory usage, or speed) which of the following (or something totally different) would be best? I have a hunch that the first is correct, but would like other people's opinions.

my_dict = defaultdict(int)
for generic in iterable:
    my_dict[generic] +=1

或:

my_dict = {}
for generic in iterable:
    if generic not in my_dict:
        my_dict[generic] = 1
    else:
        my_dict[generic]+=1

或:

my_dict = {}
for generic in iterable:
    try:
        my_dict[generic] += 1
    except(KeyError):
        my_dict[generic] = 1

同样可以说使用 my_dict = defaultdict(list) 和使用附加函数.假设使用了多个 for 循环或其他条件,而不是简单地计算单个迭代中的通用值,因为这显然具有不同的特征.

Same can be said of using my_dict = defaultdict(list) and using append functions. Assume that multiple for loops, or other conditionals are used rather than simply counting generic values from a single iterable as that would obviously have different features.

推荐答案

正如 Paulo Almeida 所评论的,对于您发布的明显"解决方案的示例是使用 collections.Counter:

As Paulo Almeida commented, for the example you posted the "obvious" solution is to use a collections.Counter:

from collections import Counter
my_dict = Counter(iterable)

就是这样.

至于您发布的其他片段,并假设 my_dict[key] += 1 仅用于示例,您的一般问题是关于如何最好地填充 dict":collections.defaultdict 是同类字典(所有键的值类型相同)的正确选择,其中类型具有默认值(数字零、空字符串、空列表...).我能想到的最常见的用例是填充列表(或集合或其他容器)的字典.

As for the other snippets you posted, and assuming the my_dict[key] += 1 was just for the example and your general question is about "how to best populate a dict": collections.defaultdict is the right choice for homogeneous dicts (same type of values for all keys) where the type has a default value (numeric zero, empty string, empty list...). The most common use case I can think of is for populating a dict of lists (or sets or other containers).

现在,当 collections.Countercollections.defaultdict 都不能解决您的问题时,您有三种可能的模式:

Now when neither collections.Counter nor collections.defaultdict solve your problem, you have three possible patterns:

  • 先睹为快
  • 尝试/排除 KeyError
  • dict.setdefault(key, value)

如果您希望密钥已经存在,try/except 解决方案会更快 - try/except 块的设置非常快,但在引发异常时代价高昂.就我而言,我不推荐它,除非您非常非常确定您的数据现在是什么样子它们将来会是什么样子.

The try/except solution will be faster if you expect the key to already exist - a try/except block is very quick to setup but costly when the exception is raised. As far as I'm concerned I don't recommand it unless you are very very very sure about what your data looks like now and what they will look like in the future.

先看"解决方案的成本几乎不变,虽然不是免费的,但仍然很便宜.这确实是您最安全的选择.

The "look before" solution has an almost constant cost, and while not free it's still quite cheap. That's really your safest bet.

dict.setdefault() 解决方案的成本与先看"解决方案的成本大致相同,但您也有实例化默认对象的恒定成本,这通常会立即遭到破坏.这是几年前的常见模式,但自从 collection.defaultdict 出现后,它的用处相当有限,并不是说大部分没用.

the dict.setdefault() solution has about the same cost as the "look before" one, BUT you also have the constant cost of instanciating a default object, that will often be thrashed immediatly. It was a common pattern some years ago but since the collection.defaultdict appeared it's of rather marginal use, not to say mostly useless.

这篇关于使用 defaultdict 替换 python 中的 try 和/或 if 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆