简体   繁体   中英

How can I append other data in a list of dictionaries based on matching key values using Python

I have a list containing dictionaries. The below piece of code is an example.

[{
    'state': 'Andaman and Nicobar Islands',
    'district': 'Nicobars',
    'chartdata': [{
        'name': 'No of Confirmed Cases',
        'data': [4982, 4983, 4988, 4989, 4991, 4991, 4992, 4993, 4993, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4997, 5000, 5001, 5005, 5007, 5007, 5007, 5007, 5009, 5009, 5009, 5013, 5014, 5014, 5014, 5014, 5014, 5015, 5016, 5016, 5016, 5017, 5018, 5020, 5020, 5020, 5022, 5024, 5024, 5024, 5024, 5026, 5028, 5028, 5028, 5029, 5030, 5031, 5031, 5032, 5035, 5036, 5038, 5038, 5039, 5039, 5039, 5041, 5042, 5043, 5044, 5046, 5052, 5081, 5083, 5084, 5084, 5098, 5109, 5116, 5123, 5131, 5149, 5161, 5175, 5190, 5201, 5209, 5247, 5262, 5289, 5331, 5390, 5421, 5466, 5490, 5527, 5569, 5614, 5665, 5716, 5764, 5816, 5875, 5949, 6046, 6084, 6150, 6181, 6223, 6255, 6311, 6341, 6367, 6398, 6426, 6470, 6510, 6542, 6568, 6603, 6638, 6674, 6709, 6758, 6789, 6820, 6844, 6853, 6878, 6901, 6917, 6936, 6964, 6984, 7005, 7018, 7043, 7070, 7088, 7105, 7119, 7131, 7154, 7168, 7197, 7233, 7243, 7261, 7269, 7280, 7306, 7335, 7363, 7394, 7406, 7415, 7425, 7438, 7440, 7441, 7446, 7455, 7462, 7466, 7467, 7469, 7475, 7478, 7482, 7482, 7487, 7491, 7492, 7492, 7492, 7492, 7493, 7496, 7499, 7501, 7505, 7506, 7510, 7516, 7518, 7521, 7525, 7525, 7525, 7529, 7530, 7530, 7531, 7534, 7535, 7537, 7539, 7539, 7540, 7541, 7541, 7541, 7544, 7546, 7546, 7548, 7548, 7548, 7548, 7548, 7548, 7549, 7549, 7550, 7553, 7556, 7557, 7557, 7559, 7560, 7560, 7560, 7560, 7560, 7564, 7565, 7566, 7566, 7567, 7570, 7572, 7572, 7572, 7573, 7576, 7577, 7579, 7580, 7582, 7584, 7592, 7592, 7595, 7596, 7598, 7600, 7602, 7607, 7615, 7616, 7617, 7618, 7618, 7619, 7620, 7620, 7621, 7621, 7622, 7623, 7624, 7626, 7627, 7629, 7629, 7632, 7633, 7634, 7635, 7637, 7637]
    }, {
        'name': 'No of Vaccinations',
        'data': []
    }]
}, {
    'state': 'Andhra Pradesh',
    'district': 'Anantapur',
    'chartdata': [{
        'name': 'No of Confirmed Cases',
        'data': [67553, 67558, 67558, 67562, 67570, 67578, 67593, 67604, 67614, 67614, 67621, 67629, 67633, 67638, 67641, 67644, 67648, 67651, 67656, 67661, 67667, 67673, 67676, 67677, 67678, 67680, 67683, 67689, 67689, 67695, 67697, 67698, 67702, 67704, 67715, 67718, 67721, 67722, 67724, 67735, 67740, 67746, 67753, 67757, 67760, 67770, 67781, 67785, 67795, 67801, 67815, 67815, 67819, 67823, 67829, 67841, 67848, 67857, 67866, 67891, 67906, 67919, 67924, 67946, 67986, 68012, 68041, 68077, 68133, 68179, 68214, 68250, 68307, 68356, 68422, 68483, 68509, 68545, 68604, 68627, 68698, 68900, 69031, 69198, 69331, 69540, 69656, 69784, 70081, 70415, 70728, 71148, 71453, 71609, 71884, 72733, 73522, 74723, 75789, 76884, 77279, 77981, 79272, 80810, 82692, 84414, 85717, 86875, 89043, 91347, 93218, 94997, 96738, 98777, 99416, 101408, 103593, 105673, 107886, 110861, 114217, 116311, 119115, 121449, 123243, 125061, 126848, 128694, 129741, 130775, 132651, 134123, 135315, 136539, 137754, 138298, 139251, 140757, 141798, 142663, 143665, 144660, 145195, 146113, 147073, 147979, 148677, 149227, 149594, 149866, 150219, 150646, 151005, 151321, 151647, 152033, 152161, 152425, 152661, 152944, 153168, 153348, 153536, 153602, 153792, 153957, 154098, 154228, 154328, 154465, 154525, 154616, 154718, 154813, 154898, 154982, 155088, 155125, 155206, 155275, 155355, 155407, 155480, 155578, 155614, 155687, 155756, 155825, 155870, 155937, 155978, 155994, 156043, 156113, 156174, 156225, 156272, 156331, 156349, 156379, 156424, 156460, 156494, 156536, 156578, 156594, 156622, 156673, 156713, 156740, 156771, 156794, 156799, 156809, 156838, 156859, 156890, 156907, 156936, 156947, 156978, 157004, 157030, 157056, 157068, 157103, 157105, 157126, 157166, 157188, 157212, 157233, 157250, 157253, 157270, 157296, 157319, 157353, 157371, 157390, 157397, 157413, 157441, 157461, 157479, 157497, 157507, 157508, 157516, 157520, 157526, 157539, 157550, 157563, 157571, 157583, 157597, 157606, 157611, 157618, 157619, 157620, 157629, 157641, 157664, 157673, 157680, 157689, 157691, 157706, 157709, 157709]
    }, {
        'name': 'No of Vaccinations',
        'data': []
    }]
}]

What I am trying to do is add another list to the empty list in the above code if the state/district matches.

{
        'name': 'No of Vaccinations',
        'data': []
    }

from another dataset using loop I have extracted the following item . and the list in the item needs to be added to the empty vaccine list in the first sample

1
Andaman and Nicobar Islands
Nicobars
[67553, 67558, 67558, 67562, 67570, 67578, 67593, 67604, 67614, 67614, 67621, 67629]

2
Andhra Pradesh
Anantapur
[0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 244, 244, 390, 390, 392, 392, 719, 719, 719]

Expected outcome is:

[{
    'state': 'Andaman and Nicobar Islands',
    'district': 'Nicobars',
    'chartdata': [{
        'name': 'No of Confirmed Cases',
        'data': [4982, 4983, 4988, 4989, 4991, 4991, 4992, 4993, 4993, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4994, 4997, 5000, 5001, 5005, 5007, 5007, 5007, 5007, 5009, 5009, 5009, 5013, 5014, 5014, 5014, 5014, 5014, 5015, 5016, 5016, 5016, 5017, 5018, 5020, 5020, 5020, 5022, 5024, 5024, 5024, 5024, 5026, 5028, 5028, 5028, 5029, 5030, 5031, 5031, 5032, 5035, 5036, 5038, 5038, 5039, 5039, 5039, 5041, 5042, 5043, 5044, 5046, 5052, 5081, 5083, 5084, 5084, 5098, 5109, 5116, 5123, 5131, 5149, 5161, 5175, 5190, 5201, 5209, 5247, 5262, 5289, 5331, 5390, 5421, 5466, 5490, 5527, 5569, 5614, 5665, 5716, 5764, 5816, 5875, 5949, 6046, 6084, 6150, 6181, 6223, 6255, 6311, 6341, 6367, 6398, 6426, 6470, 6510, 6542, 6568, 6603, 6638, 6674, 6709, 6758, 6789, 6820, 6844, 6853, 6878, 6901, 6917, 6936, 6964, 6984, 7005, 7018, 7043, 7070, 7088, 7105, 7119, 7131, 7154, 7168, 7197, 7233, 7243, 7261, 7269, 7280, 7306, 7335, 7363, 7394, 7406, 7415, 7425, 7438, 7440, 7441, 7446, 7455, 7462, 7466, 7467, 7469, 7475, 7478, 7482, 7482, 7487, 7491, 7492, 7492, 7492, 7492, 7493, 7496, 7499, 7501, 7505, 7506, 7510, 7516, 7518, 7521, 7525, 7525, 7525, 7529, 7530, 7530, 7531, 7534, 7535, 7537, 7539, 7539, 7540, 7541, 7541, 7541, 7544, 7546, 7546, 7548, 7548, 7548, 7548, 7548, 7548, 7549, 7549, 7550, 7553, 7556, 7557, 7557, 7559, 7560, 7560, 7560, 7560, 7560, 7564, 7565, 7566, 7566, 7567, 7570, 7572, 7572, 7572, 7573, 7576, 7577, 7579, 7580, 7582, 7584, 7592, 7592, 7595, 7596, 7598, 7600, 7602, 7607, 7615, 7616, 7617, 7618, 7618, 7619, 7620, 7620, 7621, 7621, 7622, 7623, 7624, 7626, 7627, 7629, 7629, 7632, 7633, 7634, 7635, 7637, 7637]
    }, {
        'name': 'No of Vaccinations',
        'data': [67553, 67558, 67558, 67562, 67570, 67578, 67593, 67604, 67614, 67614, 67621, 67629]
    }]
}, {
    'state': 'Andhra Pradesh',
    'district': 'Anantapur',
    'chartdata': [{
        'name': 'No of Confirmed Cases',
        'data': [67553, 67558, 67558, 67562, 67570, 67578, 67593, 67604, 67614, 67614, 67621, 67629, 67633, 67638, 67641, 67644, 67648, 67651, 67656, 67661, 67667, 67673, 67676, 67677, 67678, 67680, 67683, 67689, 67689, 67695, 67697, 67698, 67702, 67704, 67715, 67718, 67721, 67722, 67724, 67735, 67740, 67746, 67753, 67757, 67760, 67770, 67781, 67785, 67795, 67801, 67815, 67815, 67819, 67823, 67829, 67841, 67848, 67857, 67866, 67891, 67906, 67919, 67924, 67946, 67986, 68012, 68041, 68077, 68133, 68179, 68214, 68250, 68307, 68356, 68422, 68483, 68509, 68545, 68604, 68627, 68698, 68900, 69031, 69198, 69331, 69540, 69656, 69784, 70081, 70415, 70728, 71148, 71453, 71609, 71884, 72733, 73522, 74723, 75789, 76884, 77279, 77981, 79272, 80810, 82692, 84414, 85717, 86875, 89043, 91347, 93218, 94997, 96738, 98777, 99416, 101408, 103593, 105673, 107886, 110861, 114217, 116311, 119115, 121449, 123243, 125061, 126848, 128694, 129741, 130775, 132651, 134123, 135315, 136539, 137754, 138298, 139251, 140757, 141798, 142663, 143665, 144660, 145195, 146113, 147073, 147979, 148677, 149227, 149594, 149866, 150219, 150646, 151005, 151321, 151647, 152033, 152161, 152425, 152661, 152944, 153168, 153348, 153536, 153602, 153792, 153957, 154098, 154228, 154328, 154465, 154525, 154616, 154718, 154813, 154898, 154982, 155088, 155125, 155206, 155275, 155355, 155407, 155480, 155578, 155614, 155687, 155756, 155825, 155870, 155937, 155978, 155994, 156043, 156113, 156174, 156225, 156272, 156331, 156349, 156379, 156424, 156460, 156494, 156536, 156578, 156594, 156622, 156673, 156713, 156740, 156771, 156794, 156799, 156809, 156838, 156859, 156890, 156907, 156936, 156947, 156978, 157004, 157030, 157056, 157068, 157103, 157105, 157126, 157166, 157188, 157212, 157233, 157250, 157253, 157270, 157296, 157319, 157353, 157371, 157390, 157397, 157413, 157441, 157461, 157479, 157497, 157507, 157508, 157516, 157520, 157526, 157539, 157550, 157563, 157571, 157583, 157597, 157606, 157611, 157618, 157619, 157620, 157629, 157641, 157664, 157673, 157680, 157689, 157691, 157706, 157709, 157709]
    }, {
        'name': 'No of Vaccinations',
        'data': [0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 33, 244, 244, 390, 390, 392, 392, 719, 719, 719]
    }]
}]

I am not sure how to do this. Any help will be appreciated.

让要填充的字典是a_dict,必须插入a的字典是b_dict,你可以用下面的语句来解决上面提到的问题,

a_dict[1]['chartdata'][1]['data'] = b_dict

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM