简体   繁体   English

如何使用函数自动从非现有键创建值

[英]How to automatically create value from non existing key with a function

Backgroud: 背景:

Let's say I have a function (of course in reality that would be a more complex function): 假设我有一个函数(当然在实际中这将是一个更复杂的函数):

def f(x):
    return str(x)

If I want to store values to avoid unnecessary recomputation, I can create a dict like this: 如果我想存储值以避免不必要的重新计算,我可以创建一个像这样的dict

my_dict = {x: f(x) for x in range(5)}

But then if I don't know in advance which values I may need, for example 10 , my_dict[10] obviously generates a KeyError . 不过,如果我不事先知道哪些值我可能需要,例如10my_dict[10]显然产生一个KeyError

One way around this could be: 解决这个问题的一种方法是:

my_dict = {}
def get_value(x):
    if x not in my_dict:
        my_dict[x] = f(x)
    return my_dict[x]

get_value(10)

Question: This seems very similar to defaultdict : is there a way to make the intuitive (but broken) my_dict = defaultdict(f) work, ie when a key x doesn't exist, it should call f(x) instead of f() to create the default value? 问题:这看起来与defaultdict非常相似:有没有办法使直观(但破坏) my_dict = defaultdict(f)工作,即当一个键x不存在时,它应该调用f(x)而不是f()创建默认值?

Based on the docs , you might be able to get the behavior you want by subclassing defaultdict and overriding __missing__ : 根据文档 ,您可以通过__missing__ defaultdict并覆盖__missing__来获得所需的行为:

from collections import defaultdict
class betterdefault(defaultdict):
    def __missing__(self, key):
        return self.default_factory(key)

Now, you'd want to flesh that out with a little extra logic, eg throw KeyError if self.default_factory is None, stuff like that which they mention in the docs. 现在,你想要充实说出来一点点额外的逻辑,如抛出KeyError ,如果self.default_factory是无,这样的东西,他们在文档提到。 Hope this points you in the right direction. 希望这能指出你正确的方向。

Here's a quick demo 这是一个快速演示

you can build your own dict data type. 您可以构建自己的dict数据类型。 in your case, __missing__ will help. 在你的情况下, __missing__将有所帮助。 if there is no key, __missing__ method triggers your custom work. 如果没有密钥, __missing__方法会触发您的自定义工作。 below is a simple example. 下面是一个简单的例子。

from collections import UserDict
class MyDict(UserDict):
    def __missing__(self, key):
        self[key] = 2*key
        return self[key]

if __name__ == '__main__': # test
    a = MyDict((x, 2*x) for x in range(5))
    print(a)
    # {0: 0, 1: 2, 2: 4, 3: 6, 4: 8}
    a[5]
    # 10
    print(a)
    # {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5:10}

also note that UserDict is a wrapper around dictionary objects, making you comfortable to subclassing the dictionary data types. 另请注意, UserDict是字典对象的包装器,使您可以轻松地对字典数据类型进行子类化。

check the official docs. 查看官方文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从现有列表创建字典? 键值必须是该键的重复次数 - How to create a dictionary from existing list? Key Value must be the amount of repeats for that key 从python中现有的键值对列表创建列表 - Create a list from an existing list of key value pairs in python 使用Python中的现有或不存在的键将值追加到字典中的列表 - Append value to list in dictionary with existing or non-existing key in Python 如何从文件中将值添加到现有的空值键字典? - How to add values to an existing empty value key dictionary from a file? Django模型:如果不存在外键关系,如何返回默认值? - Django models: how to return a default value in case of a non-existing foreign-key relationship? 如何将新的键值对添加到字典列表中的现有键值对中? - How to add a new key value pair to existing key value pair from list of dicts? 为不存在的键更新字典中的设置值导致 TypeError - Updating a set value in a dictionary for non existing key resulting in TypeError 创建一个新键并分配嵌套字典中现有键的值 - Create a new key and assign the value of an existing key in a nested dictionary 如何从另一个键 Python 的值创建新键 - how to create a new key from a value of another key Python 如何基于Python中的匹配键将键值对添加到另一个字典列表中的现有字典列表中 - How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM