在 Python 中更改字典中的键 |
您所在的位置:网站首页 › python字典如何修改 › 在 Python 中更改字典中的键 |
在 Python 中的字典中分配新键和删除旧键
在 Python 中使用 pop() 函数
在 Python 中使用 for 循环遍历字典并更改字典中的键
用 Python 重建一个全新的实例
在 Python 中使用 OrderedDict 类
在 Python 中使用 Pandas.DataFrame 函数
本文介绍了在 Python 中更改字典中的键的各种方法。 在 Python 中的字典中分配新键和删除旧键要在 Python 中更改字典中的键,请按照以下步骤操作。 为旧键分配一个新键。 删除旧键。参考下面的示例,它演示了这些步骤。 dict_ex[new_key] = dict_ex[old_key] del dict_ex[old_key]以下代码说明了此方法。 dict_ex = {1: "January", 2: "Febuary", 3: "March"} print("Old dictionary-", dict_ex) dict_ex[2.1] = dict_ex[2] del dict_ex[2] print("New dictionary-", dict_ex)在这里,我们看到键的值 2 被值 2.1 替换。请注意更新的键值对如何移动到字典的末尾。 输出: Old dictionary- {1: 'January', 2: 'Febuary', 3: 'March'} New dictionary- {1: 'January', 3: 'March', 2.1: 'Febuary'} 在 Python 中使用 pop() 函数要在 Python 中更改字典中的键,请参考以下步骤。 使用 pop 功能弹出旧键。按照这个例子。 pop(old_key) 为弹出的旧键分配一个新键。按照这个例子。 dict_ex[new_key] = dict_ex.pop(old_key)下面的例子说明了这一点。 dict_ex = {1: "January", 2: "Febuary", 3: "March"} print("Old dictionary-", dict_ex) dict_ex[2.2] = dict_ex.pop(2) print("New dictionary-", dict_ex)输出: Old dictionary- {1: 'January', 2: 'Febuary', 3: 'March'} New dictionary- {1: 'January', 3: 'March', 2.2: 'Febuary'}在这里,我们看到键的值 2 被值 2.2 替换。请注意更新的键值对如何移动到字典的末尾。 在 Python 中使用 for 循环遍历字典并更改字典中的键为此,你首先获取目标字典和另一个包含新键值的字典。之后,你最终会使用 for 循环相应地切换所有键。 dict_ex = {1: "Jan", 2: "Feb", 3: "Mar"} print("Old dictionary-", dict_ex) new_key_assign = {1: 111, 2: 2, 3: 3} print("New dictionary-") print(dict([(new_key_assign.get(key), value) for key, value in dict_ex.items()]))在这里,我们看到键的值 1 被值 111 替换。请注意这次更新的键值对是如何保留的。 输出: Old dictionary- {1: 'Jan', 2: 'Feb', 3: 'Mar'} New dictionary- {111: 'Jan', 2: 'Feb', 3: 'Mar'} 用 Python 重建一个全新的实例在 Python 3.7+ 字典中,你可以保留顺序。为此,按如下方式重建字典的一个全新实例。 dict_ex = {"old_key": "Jan", 2: "Feb", 3: "Mar"} {"new_key" if k == "old_key" else k: v for k, v in dict_ex.items()}这里有一个例子来证明这一点。 dict_ex = {1: "Jan", 2: "Feb", 3: "Mar"} print({"one" if k == 1 else k: v for k, v in dict_ex.items()})在这里,我们看到键的值 1 被值 one 替换。请注意这次更新的键值对是如何保留的。 输出: {'one': 'Jan', 2: 'Feb', 3: 'Mar'} 在 Python 中使用 OrderedDict 类在 Python 3.7+ 字典中,你可以通过使用 OrderedDict 和生成器表达式来保留排序。 请注意,你首先需要从 collections 导入 OrderedDict。然后,使用 OrderedDict 类。 from collections import OrderedDict dict_ex = {"old_key": "Jan", 2: "Feb", 3: "Mar"} OrderedDict(("new_key" if k == 1 else k, v) for k, v in dict_ex.items())这里有一个例子,你可以参考。 from collections import OrderedDict dict_ex = {1: "Jan", 2: "Feb", 3: "Mar"} OrderedDict(("one" if k == 1 else k, v) for k, v in dict_ex.items())在这里,我们看到键的值 1 被值 one 替换。请注意这次更新的键值对是如何保留的。 输出: OrderedDict([('one', 'Jan'), (2, 'Feb'), (3, 'Mar')]) 在 Python 中使用 Pandas.DataFrame 函数你可以使用 Pandas 在 Python 中更改字典中的键。首先,导入 pandas 库。 然后,使用 DataFrame,如下所示。 import pandas as pd old_dictionary = {"old_value": "Jan", 2: "Feb", 3: "Mar"} new_dictionary = {"new_value": "Jan", 2: "Feb", 3: "Mar"} df = pd.DataFrame([old_dictionary, new_dictionary])检查这个示例程序。 import pandas as pd dict_ex = {1: "Jan", 2: "Feb", 3: "Mar"} print(dict_ex) new_dict_ex = {11: "Jan", 2: "Feb", 3: "Mar"} df = pd.DataFrame([dict_ex, new_dict_ex]) print(new_dict_ex)在这里,我们看到键的值 1 被值 11 替换。请注意这次更新的键值对是如何保留的。 输出: {1: 'Jan', 2: 'Feb', 3: 'Mar'} {11: 'Jan', 2: 'Feb', 3: 'Mar'} |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |