2021年4月7日星期三

How to compare every value in JSON response and filter them based on some condition in python?

I have a JSON response which looks like this

response = {     "detections":[        {           "label":"apple",           "x":50,        },        {           "label":"apple",           "x":55,        },        {           "label":"apple",           "x":500,        },        {           "label":"banana",           "x":352,        }     ]  }  

I want to filter this response such that, the values where the labels are same filter out if the absolute difference between them is less than 10, otherwise keep both the responses

So I want the final result to be like this

updated_response = {               "detections":[                  {                     "label":"apple",                     "x":50,                  },                  {                     "label":"apple",                     "x":500,                  },                  {                     "label":"banana",                      "x":352,                  }               ]            }   

The second value where x=55 is removed because the absolute difference between them is less than 10, but third value with x=500 is included because the absolute difference is more than 10

Any idea how I can achieve this?

https://stackoverflow.com/questions/66996521/how-to-compare-every-value-in-json-response-and-filter-them-based-on-some-condit April 08, 2021 at 10:22AM

没有评论:

发表评论