缺少dict键的Pythonic方式 [英] Pythonic way for missing dict keys

查看:102
本文介绍了缺少dict键的Pythonic方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我很确定这次已被问过几次,但我好像不会在b
上找到它档案(谷歌似乎有几个问题

最近)。


我想知道什么是处理失踪的最pythonic方式

键和默认值。


根据我的读数,可以采取以下方法:


1 /检查之前(这个有一个特定的名字和首字母缩略词,我还没有用心去学习b $ b。


如果不是my_dict.has_key(键):

my_obj = myobject()

my_dict [key] = my_obj

else:

my_obj = my_dict [key]


2 /尝试并在出错时做出反应(这也有一个特定的名称,但是......)


尝试:

my_obj = my_dict [key]

除了AttributeError:

my_obj = myobject()

my_dict [key] = my_obj


3 / dict.get用法:


my_obj = my_dict.get(key,myobject())


我想知道哪一个是最推荐的方式?得到的用法似乎是最简单的,但是我看到的唯一问题是我认为myobject()是在调用时评估的
,所以如果初始化很昂贵你

可能会看到惊喜。


提前感谢,

../alex

-

..w(the_mindstorm)p。

Hi all!

I am pretty sure this has been asked a couple of times, but I don''t seem
to find it on the archives (Google seems to have a couple of problems
lately).

I am wondering what is the most pythonic way of dealing with missing
keys and default values.

According to my readings one can take the following approaches:

1/ check before (this has a specific name and acronym that I haven''t
learnt yet by heart)

if not my_dict.has_key(key):
my_obj = myobject()
my_dict[key] = my_obj
else:
my_obj = my_dict[key]

2/ try and react on error (this has also a specific name, but...)

try:
my_obj = my_dict[key]
except AttributeError:
my_obj = myobject()
my_dict[key] = my_obj

3/ dict.get usage:

my_obj = my_dict.get(key, myobject())

I am wondering which one is the most recommended way? get usage seems
the clearest, but the only problem I see is that I think myobject() is
evaluated at call time, and so if the initialization is expensive you
will probably see surprises.

thanks in advance,
../alex
--
..w( the_mindstorm )p.

推荐答案

2007-07-20,Alex Popescu < th *********************** @ gmail.comwrote:
On 2007-07-20, Alex Popescu <th***********************@gmail.comwrote:

大家好!


我很确定这已被问了几次,但是我好像b $ b好​​像在档案中找不到它(Google似乎我最近有几个问题。


我想知道什么是处理丢失的最pythonic方式

键和默认值。


根据我的读数,可以采取以下方法:
Hi all!

I am pretty sure this has been asked a couple of times, but I
don''t seem to find it on the archives (Google seems to have a
couple of problems lately).

I am wondering what is the most pythonic way of dealing with missing
keys and default values.

According to my readings one can take the following approaches:



还有流行的collections.defaultdict 。


通常,正常dicts的get方法就是我想要的。我只使用

默认元素字典中的隐式添加才能使用

defaultdict。


- -

Neil Cerutti

There''s also the popular collections.defaultdict.

Usually, the get method of normal dicts is what I want. I use a
defaultdict only when the implicit addition to the dictionary of
defaulted elements is what I really want.

--
Neil Cerutti


Neil Cerutti< ho ***** @ yahoo.comwrote in

news :sl ******************* @ FIAD06.norwich.edu:
Neil Cerutti <ho*****@yahoo.comwrote in
news:sl*******************@FIAD06.norwich.edu:

2007-07-20 ,Alex Popescu< th *********************** @ gmail.comwrote:
On 2007-07-20, Alex Popescu <th***********************@gmail.comwrote:

>大家好!

我很确定这已被问了好几次,但我好像在档案馆里找不到它(Google似乎有一个<最近几个问题。

我想知道什么是处理丢失的
键和默认值的最pythonic方式。

根据我的读数可以采取以下方法:
>Hi all!

I am pretty sure this has been asked a couple of times, but I
don''t seem to find it on the archives (Google seems to have a
couple of problems lately).

I am wondering what is the most pythonic way of dealing with missing
keys and default values.

According to my readings one can take the following approaches:



还有流行的collections.defaultdict。


通常,得到诺玛的方法我的意思是我想要的。我只使用

默认元素字典中的隐式添加才能使用

defaultdict。


There''s also the popular collections.defaultdict.

Usually, the get method of normal dicts is what I want. I use a
defaultdict only when the implicit addition to the dictionary of
defaulted elements is what I really want.



这看起来最接近我的需求,但在我的情况下,默认值

涉及创建一个自定义对象实例,该实例从当前获取参数

执行上下文,所以我不太确定我可以使用它。


../alex

-

.. w(the_mindstorm)p。

This looks like the closest to my needs, but in my case the default value
involves the creation of a custom object instance that is taking parameters
from the current execution context, so I am not very sure I can use it.

../alex
--
..w( the_mindstorm )p.


版本1和2与版本3不同。后者不是
为dict增加价值。


如前所述,使用:

1 - 如果你期望dict中没有键

2 - 如果你期望dict中有钥匙

Version 1 and 2 do different thing than version 3. The latter doesn''t
add value to dict.

As it was mentioned before, use:
1 - if you expect that there''s no key in dict
2 - if you expect that there is key in dict


这篇关于缺少dict键的Pythonic方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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