📜  python 字典重命名键 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:55.757000             🧑  作者: Mango

代码示例1
def rename_keys(dict_, new_keys):
    """
     new_keys: type List(), must match length of dict_
    """

    # dict_ = {oldK: value}
    # d1={oldK:newK,} maps old keys to the new ones:  
    d1 = dict( zip( list(dict_.keys()), new_keys) )

          # d1{oldK} == new_key 
    return {d1[oldK]: value for oldK, value in dict_.items()}