# 定义字典
student_score = {
'Tom':85,
'Jerry':63,
'Marry':57,
'Jack':70
}
# 字典解析
new_student = {name:score for (name,score) in student_score.items()}
# 及格的每人的成绩都加5分(加条件的解析式)
new_student_if = {name:score + 5 for (name,score) in student_score.items() if score >= 60}
print(new_student_if) # {'Tom': 90, 'Jerry': 68, 'Jack': 75}
# 字典合并
student_score_2 = {
'math':99,
'name':'HanMM',
'Gender':'Male',
'Tom':100
}
two_dict = {**student_score,**student_score_2}
print(two_dict) #打印结果: {'Tom': 100, 'Jerry': 63, 'Marry': 57, 'Jack': 70, 'math': 99, 'name': 'HanMM', 'Gender': 'Male'}
python 字典解析
于 2023-12-14 16:59:33 首次发布