简体   繁体   中英

Ordered Dictionary python replace key value

a = OrderedDict([(14, (2361.42, 1339.25)), (15, (3581.08, 1427.08)), (16, (0, 4640.11)), (17, (0, 2667.36)), (18, (0, 1686.63))])

b =  OrderedDict([(14, '03-31'), (15, '04-07'), (16, '04-14'), (17, '04-21'), (18, '04-28')]) 

I want to replace the value in b, to a value along with b value I have tried this

dict((b[key], value) for (key, value) in a.items())

the out put is

{'03-31': (2361.42, 1339.25), '04-07': (3581.08, 1427.08), '04-28': (0, 1686.63), '04-21': (0, 2667.36), '04-14': (0, 4640.11)}

I want it as

OrderedDict([('03-31', (2361.42, 1339.25)), ('04-07', (3581.08, 1427.08)), (04-14', (0, 4640.11)), ('04-21', (0, 2667.36)), ('04-28', (0, 1686.63))])

您可以使用OrderedDict构造函数:

OrderedDict((b[key], value) for (key, value) in a.items())

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