字典的切片方法


ai_disease_profile = {
    
    'id1': 0,
 'id2': 0,
 'id3': 0,
 'id4': 0,
 'id7': 0,
 'id10': 0,
 'id12': 0,
 'others': 0,
 'all_num': 0,
 'blood_area_ratio': 0,
 'max_blood_area_ratio': 0}
# 字典的切片方法
targets = ['id1', 'id2', 'id3', 'id4', 'id7','id10','id12','others']
result = {
    
    key: ai_disease_profile[key] for key in ai_disease_profile.keys() if key in targets}
>>>result
{
    
    'id1': 0,
 'id2': 0,
 'id3': 0,
 'id4': 0,
 'id7': 0,
 'id10': 0,
 'id12': 0,
 'others': 0}

猜你喜欢

转载自blog.csdn.net/weixin_42782150/article/details/128150013