This is easy enough to do this with a few loops but I am sure there is a far more efficient way to achieve this and I am keen to learn.
Consider the following array of dict which represents data pulled from a nosql database.
x = [
{
"loc" : "alpha",
"tag" : 1,
"dist" : 5
},
{
"loc" : "bravo",
"tag" : 0,
"dist" : 2
},
{
"loc" : "charlie",
"tag" : 5,
"dist" : 50
},
{
"loc" : "delta",
"tag" : 4,
"dist" : 2
},
{
"loc" : "echo",
"tag" : 2,
"dist" : 30
},
{
"loc" : "foxtrot",
"tag" : 4,
"dist" : 2
},
{
"loc" : "gamma",
"tag" : 4,
"dist" : 2
},
{
"loc" : "hotel",
"tag" : 0,
"dist" : 2
},
]
I would like to find all the items with the lowest 'dist' value, and should there be more than one dict with the same lowest value, I want the grouping on the attribute 'tag' that has the most dicts with the same lowest value.
for example, the desired returned data from the above would be:
r = [
{
"LocationName" : "delta",
"tag" : 4,
"dist" : 2
},
{
"loc" : "foxtrot",
"tag" : 4,
"dist" : 2
},
{
"loc" : "gamma",
"tag" : 4,
"dist" : 2
}
]
Summary: dist:2 is the lowest value, [bravo, delta, foxtrot, gamma, hotel] all have a dist of 2, [bravo, hotel] have a tag of:0 and [delta, foxtrot, gamma] have a tag of:4 . An array of the dicts [delta, foxtrot, gamma] are returned as they have more with the same matching tag and the lowest dist.
I am using python 3.6.
Thank you for the help and interest!