简体   繁体   中英

What is the most efficient way to edit the values in a list of dictionaries?

I have multiple dictionaries inside the list, what is an efficient and possible way to update and limit all the float values to only two decimal points? For example: Make the value of 'AmazonEC2': 22.740000000000002 to 'AmazonEC2': 22.74

[{
  'AmazonEC2': 22.740000000000002,
  'awskms': 0.09,
  'AmazonDynamoDB': 6.740000000000002,
  'AmazonElastiCache': 0.01,
  'AmazonS3': 5.54,
  'AmazonCloudWatch': 1.08,
  'AWSAmplify': 0.55,
  'AmazonRDS': 0.01
}, {
  'awskms': 0.740000000000003,
  'AmazonS3': 5.740000000000004,
  'AmazonCloudWatch': 1.740000000000003,
  'AmazonDynamoDB': 6.740000000000006,
  'AmazonEC2': 22.740000000000002,
  'AWSAmplify': 0.49,
  'AmazonRDS': 0.01,
  'AmazonElastiCache': 0.01
}]
for item in your_list:
    for key in item.keys():
        item[key] = round(item[key], 2)

The main iteration is list, dict lookup is O(1) .

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