相关疑难解决方法(0)

如何在Python中区分两个词典?

我有两本词典.我需要找到两者之间的区别,它应该给我关键和价值.

我搜索并发现了一些像datadiff,dictdiff-master这样的插件/软件包,但是当我在Python 2.7中尝试它时,它说没有定义这样的模块.

我在这里使用了set.

first_dict = {}
second_dict = {}

value = set(second_dict)-set(first_dict)
print value
Run Code Online (Sandbox Code Playgroud)

输出>>>设置(['SCD-3547','SCD-3456'])

我只获得关键,我甚至需要获得价值.

python

44
推荐指数
13
解决办法
6万
查看次数

当提供的参数为 None 时,是否可以强制数据类字段调用其 default_factory ?

我有一个带有可变字段(列表)的数据类。我希望实现的是,即使在 __init__ 调用中显式设置为 None ,该字段也永远不会为 None 。在普通的类中,实现起来很简单:

class A:
    def __init__(self, l: Optional[List[int]] = None):
        if l is None:
            l = []
        self.l = l
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以仅使用dataclasses.field函数来实现相同的结果,即无需显式实现 __init__ 方法(​​当类有很多属性时,这会很麻烦)?default_factory当提供的 init 参数为 None 时,我可以强制 dataclasses.field 调用它吗?

python python-dataclasses

5
推荐指数
1
解决办法
2895
查看次数

标签 统计

python ×2

python-dataclasses ×1