简体   繁体   中英

In python, how do I get the lowest key value pair in a list of objects/dicts where each item has a different, separate key value pair?

I'm kind of a beginner in python, so there are some things I am used to in Java and C++ that I'm not sure how to do in python. One of these things is to return a minimum value from an array/list. This seems to work with lists that aren't defined as dicts, but as you can see below, my final output list is actually a list of dicts, and I'm not sure how to go about this.

I want to have a list of dynamically generated key, value pairs, such that the things that are added to the list are coupled together from the get go so that I know what the item in the list actually means. That part I am getting. The problem is that I have too many values to comb through to find the value I'm actually looking for, which is the key value pair with the lowest value, regardless of what the key is.

I have already tried changing the output from a list to a defaultdict(list) and changing the way that values are appended to it, but that leads to an undesirable output. The only method that seems to work that couples the two outputs I want is to add an item in the format: {key: value} to a list and then try to sort the list by value in descending order and then pick the first one; and unfortunately I can't even figure out how to do that because it seems like indexing in python is different.

I have a function that looks like this:

def getnumdistance(q, r)
  #gets numerical distance between two vectors from a csv file

def comparetotrain(testexample, trainlist):
  distancelist = []
  for example in trainlist:
    for key, value in example.items():
      if (key == "Class"):
        distancelist.append({
          value: getnumdistance(testexample, example)
          })
  return distancelist

that outputs a list of things that look like this:

[{'High': 5.314132102234569},
{'MidHigh': 8.029701115234614},
{'Low': 24.002408212510677},
...
{'MidHigh': 0.0}
...
{'High': 9.20044020685967},
{'LowMid': 22.120942565813056}]

And I want to output only the minimum value from distancelist[] because I plan on only using that to check my knn error rate (in this case, {MidHigh: 0.0} ).

I plan on iterating through many different elements in trainlist , but I don't want to have to comb through them every time to try and find the minimum value.

Please help if you can.

my_list = [{'High': 5.314132102234569},
{'MidHigh': 8.029701115234614},
{'Low': 24.002408212510677},
{'MidHigh': 0.0},
{'High': 9.20044020685967},
{'LowMid': 22.120942565813056}]

sorted(my_list,key=lambda x:list(x.values())[0])

This will sort the list.

Use min() to find minimum of list with its key parameter specifying on what basis you need to estimate the minimum:

lst = [{'High': 5.314132102234569},
{'MidHigh': 8.029701115234614},
{'Low': 24.002408212510677},
{'MidHigh': 0.0},
{'High': 9.20044020685967},
{'LowMid': 22.120942565813056}]

print(min(lst, key=lambda x: list(x.values())))
# {'MidHigh': 0.0}

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