Python字典DataStructure方法d []还是d.get()? [英] Python Dictionary DataStructure which method d[] or d.get()?
问题描述
虽然使用Python字典DataStructure(包含键值对)如果我想从我的字典中检索一些值,我有两个选项 d ['']和g.get('key')
While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve some value from my Dictionary i have two options d[''] and g.get('key') so i am confused now which is better and Why ?? I understand both some way but when it comes to memory consumption and evaluation in memory which one is better ??
希望对一些积极的答复,
Hoping for some Positive reply,
请问。
推荐答案
d [key]
用键键返回 d 的项目。如果键不在地图中,则会引发KeyError
。
d[key]
Return the item of d with key key. Raises aKeyError
if key is not in the map.
如果dict的子类定义了一个方法 __缺少__()
,如果密钥键不存在, d [key]
操作将键入键作为参数调用该方法。 d [key]
操作然后返回或引发由 __缺少__(键)
调用返回或引发的任何值,如果钥匙不存在没有其他操作或方法调用 __缺少__()
。如果 __缺少__()
未定义,则会引发 KeyError
。 __缺少__()
必须是一种方法;它不能是一个实例变量。 [...]
If a subclass of dict defines a method __missing__()
, if the key key is not present, the d[key]
operation calls that method with the key key as argument. The d[key]
operation then returns or raises whatever is returned or raised by the __missing__(key)
call if the key is not present. No other operations or methods invoke __missing__()
. If __missing__()
is not defined, KeyError
is raised. __missing__()
must be a method; it cannot be an instance variable. [...]
和
get(key [,default])
如果键返回键的值在字典中,否则默认。如果没有给出默认值,则默认为None
,以便此方法不会引发KeyError
。
get(key[, default])
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults toNone
, so that this method never raises aKeyError
.
区别在于返回值。当您要求与不存在的密钥对应的值时,您需要
The difference lies in the return value. When you ask for the value corresponding to a non-existing key, you either want
- A
KeyError
raise - 回调调用
- 返回的默认值
- A
KeyError
raised - A callback invoked
- A default value returned
Python通过多种方法提供不同的功能。
Python provides the different functionalities through multiple methods.
使用 []
当找不到密钥时,无论是在调用 _missing _
还是引发异常。当密钥IS存在时,哪个是更快的,我检查了源代码。 (我使用2.7.2进行此检查。)在 dictobject.c
中,我们看到:
There will be a performance hit using []
when the key is not found, either in calling _missing_
or raising the exception. As to which one is faster when the key IS present, I checked the source code. (I used 2.7.2 for this check.) In dictobject.c
we see:
-
get
calldict_get
-
]
调用dict_subscript
get
callsdict_get
[]
callsdict_subscript
现在如果值存在于 dict_get
我们有
Now if the values are present, in dict_get
we have
if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
return NULL;
if (!PyString_CheckExact(key) ||
(hash = ((PyStringObject *) key)->ob_shash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
}
ep = (mp->ma_lookup)(mp, key, hash);
和 dict_subscript
我们有
assert(mp->ma_table != NULL);
if (!PyString_CheckExact(key) ||
(hash = ((PyStringObject *) key)->ob_shash) == -1) {
hash = PyObject_Hash(key);
if (hash == -1)
return NULL;
ep = (mp->ma_lookup)(mp, key, hash);
唯一的区别是 get
额外的打包元组!
The only difference is that get
does an extra unpack tuple!
重要?我不知道。 : - )
Significant? I have no idea. :-)
这篇关于Python字典DataStructure方法d []还是d.get()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!