Python字典DataStructure方法d []还是d.get()? [英] Python Dictionary DataStructure which method d[] or d.get()?

查看:122
本文介绍了Python字典DataStructure方法d []还是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,

请问。

推荐答案

Python Library Docs


d [key]

用键键返回 d 的项目。如果不在地图中,则会引发 KeyError

d[key]
Return the item of d with key key. Raises a KeyError 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 to None, so that this method never raises a KeyError.

区别在于返回值。当您要求与不存在的密钥对应的值时,您需要

The difference lies in the return value. When you ask for the value corresponding to a non-existing key, you either want


  1. A KeyError raise

  2. 回调调用

  3. 返回的默认值

  1. A KeyError raised
  2. A callback invoked
  3. 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 call dict_get

  • ] 调用 dict_subscript

  • get calls dict_get
  • [] calls dict_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屋!

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