如何在python中覆盖字典键名

时间:2015-10-13 00:26:53

标签: python dictionary

我有这个代码用于替换另一个字典中的密钥

def overwriteKeys(data, type=2):
        d = (
            ('user1', 'user11'),
            ('user2', 'user21'),
            ('user3', 'user31'),
            ('user4', 'user1'),
            ('user5', 'user51'),
        )

        d = collections.OrderedDict(d)


        data1 = data.copy()

        for k1 in data:
            for k2 in d:
                if k1 == k2:
                    key = d[k2]
                    data1[key] = data[k1]
                    del data1[k1]
        return data1

目前它取代了有价值的密钥。但我希望如果type=1那么它应该替换另一种方式,value with key。我也在寻找一些简短的代码

编辑:

d = {
    "user1" : "john"
    "user2" : "tom"   
}

will become

d = {
    "user11" : "john"
    "user21" : "tom"   
}

如果type = 1那么

d = {
    "user11" : "john"
    "user2" : "tom"   
}

will become

d = {
    "user1" : "john"
    "user2" : "tom"   
}

5 个答案:

答案 0 :(得分:4)

>>> t2 = dict([
...         ('user1', 'user11'),
...         ('user2', 'user21'),
...         ('user3', 'user31'),
...         ('user4', 'user1'),
...         ('user5', 'user51'),
...     ])
>>> 
>>> d = {
...     "user1": "john",
...     "user2" : "tom"   
... }
>>> 
>>> {t2.get(k, k): v for k, v in d.items()}
{'user11': 'john', 'user21': 'tom'}

您可以为此反向转换创建一个dict

t1 = {v: k for k, v in t2.items()}

所以最后你会得到像

这样的东西
def overwriteKeys(data, type=2):
    t2 = dict([
        ('user1', 'user11'),
        ('user2', 'user21'),
        ('user3', 'user31'),
        ('user4', 'user1'),
        ('user5', 'user51'),
    ])

    t1 = {v: k for k, v in t2.items()}
    t = t1 if type == 1 else t2
    return {t.get(k, k): v for k, v in data.items()}

dataOrderedDict

的情况下保留订单
    return OrderedDict((t.get(k, k), v) for k, v in data.items())

答案 1 :(得分:2)

您实际上并没有“覆盖”原始示例中的任何内容 - 因此,您可以构建一个使用查找值的新dict,否则它将使用原始值。至于反向查找 - 在构建dict之前反转元组的元素,例如:

def change_keys(data, type=2):
    d = (
        ('user1', 'user11'),
        ('user2', 'user21'),
        ('user3', 'user31'),
        ('user4', 'user1'),
        ('user5', 'user51'),
    )

    if type == 1:
        d = tuple(el[::-1] for el in d)

    return {d.get(k, k):v for k, v in OrderedDict(d).items()}

示例:

d = {"user1" : "john", "user2" : "tom"}
print(change_keys(d), change_keys(d, 1))
# {'user11': 'john', 'user21': 'tom'} {'user4': 'john', 'user2': 'tom'}

答案 2 :(得分:0)

最简单的思考方法是使用单独的字典,将值映射到键。

original_dict = {"a":"x", "b":"y"}
new_dict = {}
for k in original_dict:
    new_dict[original_dict[k]]=k

做完你的魔法! :)

答案 3 :(得分:0)

def overwriteKeys(data, type=2):
        d = (
            ('user1', 'user11'),
            ('user2', 'user21'),
            ('user3', 'user31'),
            ('user4', 'user1'),
            ('user5', 'user51'),
        )

        d = collections.OrderedDict(d)
        if type == 2:
            d = collections.OrderedDict([(value, key) for key, value in d.iteritems()])


        data1 = data.copy()

        for k1 in data:
            for k2 in d:
                if k1 == k2:
                    key = d[k2]
                    data1[key] = data[k1]
                    del data1[k1]
        return data1

答案 4 :(得分:0)

编辑:已修复,以便遗失项目:

stopword.contains(word)