python字典构造中的奇怪语法-您能解释吗?

问题描述 投票:1回答:1

我正在查看其他人的Python代码,并看到以下表达式:

dp = defaultdict(lambda: inf, {(): 0})

是什么意思?

我熟悉defaultdict-defaultdict(lambda: inf)将创建默认值为inf的字典。但是第二个参数呢?{(): 0}呢?它是什么意思?

我也看到了另一条类似的线:

memo={(): 0}

它看起来像字典文字,但正如我所说的,我对这种语法不熟悉。我感到困惑的是空括号。看起来像一个空的元组,但我无法在字典文字的上下文中理解它。

你能解释吗?

编辑:

我看到了following code中的第一行。因此,我想我正在尝试了解这有什么好处。作者没有响应,所以我试图了解使用此语法的原理和潜在好处。

我看到第二行here。再次,我试图了解使用此语法的好处,因为我之前从未见过。

EDIT2:我意识到第一行等效于

dp = defaultdict(lambda: 0)
dp[()] = 0

主要优点是它的简短-它可以在一行代码中完成。我只是不知道您可以定义一个defaultdict,然后同时在其中添加一个条目。

现在,第二行(使用空元组作为字典条目的键)仍然有些不寻常。但这只是一个不寻常的选择-这种语法本身没有新内容。

PS。我没有接受帕特里克的回答,即使它没有提供我所寻找的答案。它仍然给了我一些指导。

python-3.x dictionary syntax
1个回答
1
投票

它创建一个元组为(item:inf, {():0})的默认字典吗?

为什么不简单地添加一项并亲自查看?

from collections import defaultdict
from math import inf
dp = defaultdict(lambda: inf, {(): 0})

dp[42]
print(dp) 

输出:

defaultdict(<function <lambda> at 0x7f5b389a7e18>, {(): 0, 42: inf})

向帮助调用中的this添加了打印语句

from collections import Counter, defaultdict
from math import inf, isinf
from pprint import pprint

class Solution(object):
    def minStickers(self, stickers, target):
        s_cnts = *map(Counter, stickers),
        dp = defaultdict(lambda: inf, {(): 0})
        def helper(cnt):
            _id = tuple(cnt.items())
            if isinf(dp[_id]):
                dp[_id] = 1 + min((helper(cnt - s_cnt) for s_cnt in s_cnts 
                                  if s_cnt[_id[0][0]]), default=inf)
            pprint(dp)
            return dp[_id]

        # no python 3.8 available, replaced walrussoperator  
        return -1 if isinf( helper(Counter(target))) else helper(Counter(target))

s = Solution()

print(s.minStickers(["with", "example", "science"], "thehat"))

导致]

defaultdict(<function Solution.minStickers.<locals>.<lambda> at 0x03A4C4F8>,
            {(): 0,
             (('e', 1), ('a', 1)): inf,
             (('t', 1), ('h', 1), ('e', 1), ('a', 1)): inf,
             (('t', 2), ('h', 2), ('e', 1), ('a', 1)): inf})

defaultdict(<function Solution.minStickers.<locals>.<lambda> at 0x03A4C4F8>,
            {(): 0,
             (('a', 1),): inf,
             (('e', 1), ('a', 1)): inf,
             (('t', 1), ('h', 1), ('e', 1), ('a', 1)): inf,
             (('t', 2), ('h', 2), ('e', 1), ('a', 1)): inf})

defaultdict(<function Solution.minStickers.<locals>.<lambda> at 0x03A4C4F8>,
            {(): 0,
             (('a', 1),): 1,
             (('e', 1), ('a', 1)): inf,
             (('t', 1), ('h', 1), ('e', 1), ('a', 1)): inf,
             (('t', 2), ('h', 2), ('e', 1), ('a', 1)): inf})

defaultdict(<function Solution.minStickers.<locals>.<lambda> at 0x03A4C4F8>,
            {(): 0,
             (('a', 1),): 1,
             (('e', 1), ('a', 1)): 1,
             (('t', 1), ('h', 1), ('e', 1), ('a', 1)): inf,
             (('t', 2), ('h', 2), ('e', 1), ('a', 1)): inf})

defaultdict(<function Solution.minStickers.<locals>.<lambda> at 0x03A4C4F8>,
            {(): 0,
             The thread 'MainThread' (0x1) has exited with code 0 (0x0).
(('a', 1),): 1,
             (('e', 1), ('a', 1)): 1,
             (('t', 1), ('h', 1), ('e', 1), ('a', 1)): 2,
             (('t', 2), ('h', 2), ('e', 1), ('a', 1)): inf})

defaultdict(<function Solution.minStickers.<locals>.<lambda> at 0x03A4C4F8>,
            {(): 0,
             (('a', 1),): 1,
             (('e', 1), ('a', 1)): 1,
             (('t', 1), ('h', 1), ('e', 1), ('a', 1)): 2,
             (('t', 2), ('h', 2), ('e', 1), ('a', 1)): 3})
© www.soinside.com 2019 - 2024. All rights reserved.