定义的字典条目 Python

Defined dict entries Python

关于如何制作类似以下内容的任何想法?

让有:

a = {'p': 1, 'r': 2}
b = {'p': 1, 'r': 3}

这是一个简化的条目,但是假设有更多的通用键值对,因此目标是使定义更小且更易读。有没有办法给它起别名,这样我就可以写成例如:

m = ('p': 1)  # note this does not work
a = {m, 'r': 2}
b = {m, 'r': 3}

这是我的问题的混淆版本:

what = {
        sr: [{'los_angels': mg, 'sg': sg, 'ua': boston, 'new_york': seattle},
                    {'los_angels': mg, 'sg': sg, 'new_york': seattle},
                    {'los_angels': mg, 'ua': boston, 'new_york': seattle},
                    {'los_angels': mg, 'new_york': seattle},
                    {'los_angels': mg, 'ua': boston, 'new_york': seattle, 'apple': 'IS'},
                    {'los_angels': mg, 'new_york': seattle, 'apple': 'IS'}],
        as: [{'los_angels': mg, 'ua': boston, 'new_york': seattle}, {'los_angels': mg, 'new_york': seattle},
                {'los_angels': mg, 'ua': boston, 'new_york': seattle, 'apple': 'IS'},
                {'los_angels': mg, 'new_york': seattle, 'apple': 'IS'}],
        dd: [{'los_angels': 'orange', 'new_york': seattle},
                    {'los_angels': 'orange', 'new_york': seattle, 'ua': boston},
                    {'los_angels': 'orange', 'new_york': seattle, 'apple': 'IS'},
                    {'los_angels': 'orange', 'new_york': seattle, 'ua': boston, 'apple': 'IS'}],
        a: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        b: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        c: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        d: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        e: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        f: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        g: [{}],
        h: [{}],
        i: [{}, {'los_angels': mg}],
    }

所以我正在寻找一些方法来缩小 'los_angels': 'orange' 部分,'ua':boston :)

您只需要对代码进行最小的更改:

m=(('p',1),)
a=dict(m, r=2)
b=dict(m, r=3)

你不能像那样使用预定义的键值对,不,但还有其他选择:

  • 您可以定义一个基本字典,然后用它更新 ab

    m = {'p': 1}
    a = {'r': 2}
    a.update(m)
    b = {'r': 3}
    a.update(m)
    
  • 您可以使用 dict() 函数将两个词典合并为一个新词典,或者添加额外的键作为关键字参数:

    m = {'p': 1}
    a = dict(m, r=2)
    b = dict(m, r=3)
    

    这要求其他键(如 r)为 valid Python identifiers。您可以使用 ** 语法来解决该限制:

    m = {'p': 1}
    a = dict(m, **{'r': 2})
    b = dict(m, **{'r': 3})
    

    现在 r 又可以是任何字符串了。

  • 您可以将键和值定义为单独的变量,并使用它们:

    m_key, m_value = 'p', 1
    a = {m_key: m_value, 'r': 2}
    b = {m_key: m_value, 'r': 3}
    

将第二个选项应用于您的混淆样本:

la_orange = {'los_angels': 'orange'}

what = {
        sr: [{'los_angels': mg, 'sg': sg, 'ua': boston, 'new_york': seattle},
                    {'los_angels': mg, 'sg': sg, 'new_york': seattle},
                    {'los_angels': mg, 'ua': boston, 'new_york': seattle},
                    {'los_angels': mg, 'new_york': seattle},
                    {'los_angels': mg, 'ua': boston, 'new_york': seattle, 'apple': 'IS'},
                    {'los_angels': mg, 'new_york': seattle, 'apple': 'IS'}],
        as: [{'los_angels': mg, 'ua': boston, 'new_york': seattle}, {'los_angels': mg, 'new_york': seattle},
                {'los_angels': mg, 'ua': boston, 'new_york': seattle, 'apple': 'IS'},
                {'los_angels': mg, 'new_york': seattle, 'apple': 'IS'}],
        dd: [dict(la_orange, new_york=seattle),
                    dict(la_orange, new_york=seattle, ua=boston),
                    dict(la_orange, new_york=seattle, apple='IS'),
                    dict(la_orange, new_york=seattle, ua=boston, apple='IS')],
        a: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        b: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        c: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        d: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        e: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        f: [{}, {'ua': boston}, {'new_york': seattle}, {'new_york': seattle, 'ua': boston}],
        g: [{}],
        h: [{}],
        i: [{}, {'los_angels': mg}],
    }

如果您需要组合多个这样的预定义词典,您可以创建一个辅助函数:

def combine_dicts(*d, **kw):
    """Combine dictionaries into one.

    Keys in later dictionaries override those in earlier dictionaries, with
    keyword arguments being applied last.

    """
    return reduce(lambda d1, d2: dict(d1, **d2), d + (kw,))

然后将其用于:

a = combine_dicts(base1, base2, {'some non-identifier key': 42}, r=3)