在 Python 中将字典通过迭代进行修改
Python 中在遍历字典时修改字典:
-
使用
dict.copy()
方法迭代字典的副本。 - 在每次迭代中,检查是否满足条件。
- 如果满足条件,则修改字典。
my_dict = {
'first_name': 'fql',
'last_name': 'jiyik',
'site': 'jiyik.com',
'topic': 'Python'
}
for key in my_dict.copy():
if my_dict[key] == 'Python':
del my_dict[key]
my_dict['new_key'] = 'new_value'
# 👇️ {'first_name': 'fql', 'last_name': 'jiyik', 'site': 'jiyik.com', 'new_key': 'new_value'}
print(my_dict)
我们使用了 dict.copy()
方法来获取字典的副本。
dict.copy()
方法返回调用该方法的对象的浅表副本。
my_dict = {
'first_name': 'fql',
'last_name': 'jiyik',
'site': 'jiyik.com',
'topic': 'Python'
}
# 👇️ {'first_name': 'fqk', 'last_name': 'jiyik', 'site': 'jiyik.com', 'topic': 'Python'}
print(my_dict.copy())
这是必要的,因为我们不允许在迭代字典时修改字典。
但是,我们可以迭代字典的副本并修改原始字典的项目。
在每次迭代中,我们检查当前值是否等于特定字符串。
如果满足条件,我们可以使用 del
运算符从字典中删除键值对或添加新的键值对。
我们还可以迭代字典项目的副本。
my_dict = {
'first_name': 'fql',
'last_name': 'jiyik',
'site': 'jiyik.com',
'topic': 'Python'
}
for key, value in my_dict.copy().items():
if value == 'Python':
del my_dict[key]
my_dict['new_key'] = 'new_value'
# 👇️ {'first_name': 'fql', 'last_name': 'jiyik', 'site': 'jiyik.com', 'new_key': 'new_value'}
print(my_dict)
dict.items
方法返回字典项((键,值)对)的新视图。
my_dict = {
'first_name': 'fql',
'last_name': 'jiyik',
'site': 'jiyik.com',
'topic': 'Python'
}
# 👇️ dict_items([('first_name', 'fql'), ('last_name', 'jiyik'), ('site', 'jiyik.com'), ('topic', 'Python')])
print(my_dict.items())
或者,我们可以使用 list()
类。
在使用 list() 遍历字典时修改字典
在遍历字典时修改字典:
-
使用
list()
类将字典转换为键列表。 -
使用
for
循环遍历键列表。 - 如果满足特定条件,则修改字典。
my_dict = {
'first_name': 'fql',
'last_name': 'jiyik',
'site': 'jiyik.com',
'topic': 'Python'
}
for key in list(my_dict):
if my_dict[key] == 'Python':
del my_dict[key]
my_dict['new_key'] = 'new_value'
# 👇️ {'first_name': 'fql', 'last_name': 'jiyik', 'site': 'jiyik.com', 'new_key': 'new_value'}
print(my_dict)
我们使用 list()
类将字典转换为键列表。
my_dict = {
'first_name': 'fql',
'last_name': 'jiyik',
'site': 'jiyik.com',
'topic': 'Python'
}
print(list(my_dict)) # 👉️ ['first_name', 'last_name', 'site', 'topic']
print(list(my_dict.keys())) # 👉️ ['first_name', 'last_name', 'site', 'topic']
我们也可以使用 dict.keys()
方法来更明确。
dict.keys
方法返回字典键的新视图。
在每次迭代中,我们检查是否满足某个条件,如果满足,我们修改字典。
我们不是遍历字典,而是遍历键列表。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。