定义dict条目Python

时间:2015-02-03 11:38:59

标签: python python-3.x dictionary

如何制作如下内容的任何想法?

让我们:

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' orange'部分,' ub':波士顿小:)

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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

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