简体   繁体   English

定义的字典条目Python

[英]Defined dict entries Python

Any ideas how to make something like the following? 任何想法如何使像下面这样?

Let there be: 让有:

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

This is a simplified entry, however imagine that there are more common key-value pairs, so the goal is to make the definition smaller and more readable. 这是一个简化的条目,但是请想象有更多的公用键值对,因此目标是使定义更小且更易读。 Is there any way to alias it such that I can write eg: 有什么办法可以给它起别名吗,例如:

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

Here's an obfuscated version of my issue: 这是我的问题的混淆版本:

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}],
    }

So I'm looking for some way to make the 'los_angels': 'orange' part, 'ua':boston smaller :) 所以我正在寻找使'los_angels':'orange'部分,'ua':boston更小的方法:)

You cannot use a predefined key-value pair like that, no, but there are other options: 您不能使用这样的预定义键值对,否,但是还有其他选择:

  • You could define a base dictionary, then update a and b with it: 您可以定义一个基础词典,然后用它更新ab

     m = {'p': 1} a = {'r': 2} a.update(m) b = {'r': 3} a.update(m) 
  • You could use the dict() function to combine two dictionaries into one new one, or to add additional keys as keyword arguments: 您可以使用dict()函数将两个字典合并为一个新字典,或添加其他键作为关键字参数:

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

    This requires the other keys (like r ) to be valid Python identifiers . 这要求其他键(如r )为有效的Python标识符 You could use the ** syntax to work around that limitation: 您可以使用**语法来解决该限制:

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

    Now r can be any string again. 现在r可以再次是任何字符串。

  • You could define the key and value as separate variables, and use those: 您可以将键和值定义为单独的变量,然后使用它们:

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

Applying the second option to your obfuscated sample: 将第二个选项应用于混淆的样本:

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}],
    }

If you need to combine multiple such pre-defined dictionaries, you can create a helper function: 如果需要组合多个此类预定义词典,则可以创建一个辅助函数:

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

then use this with: 然后将其用于:

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

You only need a minimal change to your code: 您只需要对代码进行最小的更改:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM