python中dget函数的使用方法_Python字典DataStructure方法d []还是d.get()?

Python Library Docs

d[key]

Return the item of d with key key. Raises a KeyError if key is not in the map.

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])

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.

差异在于返回值.当您要求与不存在的键对应的值时,您要求

>提出了一个KeyError

>调用回调

>返回一个默认值

Python通过多种方法提供了不同的功能.

在没有找到密钥的情况下,使用[],或者在调用_missing_或引发异常时,会出现性能问题.关键IS存在时哪一个更快,我检查了源代码. (我使用2.7.2进行此检查.)在dictobject.c中,我们看到:

>调用dict_get

> []调用dict_subscript

现在如果值存在,在dict_get我们有

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做一个额外的解包元组!

重大?我不知道. 🙂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的例子,展示了如何使用d_hash在哈希表查找和插入目录项: ```c #include <linux/fs.h> #define HASH_SIZE 1024 struct hlist_head hash_table[HASH_SIZE]; void init_hash_table(void) { int i; for (i = 0; i < HASH_SIZE; i++) INIT_HLIST_HEAD(&hash_table[i]); } struct dentry *lookup_dentry(const char *name, struct dentry *parent) { struct hlist_node *node; struct dentry *dentry; hash_for_each_possible(hash_table, dentry, node, d_hash(name, strlen(name), parent->d_inode->i_ino)) { if (!strcmp(name, dentry->d_name.name) && dentry->d_parent == parent) { dget(dentry); return dentry; } } return NULL; } int insert_dentry(struct dentry *dentry) { struct hlist_node *node; unsigned int hash = d_hash(dentry->d_name.name, strlen(dentry->d_name.name), dentry->d_parent->d_inode->i_ino); hash_for_each_possible(hash_table, node, hash) { struct dentry *existing = hlist_entry(node, struct dentry, d_hash); if (existing->d_parent == dentry->d_parent && !strcmp(existing->d_name.name, dentry->d_name.name)) return -EEXIST; } hlist_add_head(&dentry->d_hash, &hash_table[hash]); return 0; } ``` 在上面的代码,我们首先定义了一个大小为1024的哈希表,每个桶是一个hlist_head结构。然后我们定义了一个init_hash_table函数,用于初始化哈希表。 接下来,我们定义了lookup_dentry函数,它接受一个目录项名称和其父目录项,并在哈希表查找匹配的目录项。我们使用d_hash函数计算哈希值,并使用hash_for_each_possible宏在哈希表遍历可能的目录项。对于每个匹配的目录项,我们检查名称和父目录项是否匹配,并返回匹配的目录项(如果存在)。 最后,我们定义了一个insert_dentry函数,它接受一个目录项并将其插入哈希表。我们首先使用d_hash函数计算哈希值,然后使用hash_for_each_possible遍历哈希表的可能冲突项。对于每个可能的冲突项,我们检查名称和父目录项是否匹配,如果匹配则返回-EEXIST,表示目录项已经存在。如果没有冲突项,则使用hlist_add_head将目录项添加到哈希表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值