0

I'm working on a project where I have to loop through a series of objects. Each object has numerical data linked to it regarding the distance each object relative to another object in the program. To do this I was thinking a list of dictionaries would be best. So for example these are the objects and numerical value associated with them:

a: 10, b: 5, c: 7

My issue is I have to find the object with the lowest number which I will be doing by using the min() method but after doing this I need to pull back the object name related to the minimum value to run another function on it and remove it from the list before reiterating through the list again to find the next lowest value.

I was thinking a list of dictionaries would be best to handle this but I have no idea how to compare the numerical data to get the minimum value and pull back the related object name. Each time the program is run the list can contain a different number of objects generated randomly.

[
{"a": 10},
{"b": 5},
{"c": 7}
]

In the example above I would want it to identify b as the lowest value then pull back the object associated with the lowest value to run a function on that object then remove it from the list before looping through it again to remove the next lowest value which in the case above would be c.

Is this achievable and if so how could I go about doing this?

  • 1
    Take a look at https://stackoverflow.com/questions/403421/how-to-sort-a-list-of-objects-based-on-an-attribute-of-the-objects. The object associated with the minimum value will have an index of 0 in the sorted list. – Eric Truett Apr 07 '20 at 01:07
  • A list of dictionaries isn't a great fit for this. Generally a list of dictionaries will all be the same *type*, in other words they will have the same keys so you can grab one at random and know how to get the data. Here, you don't know which key to use for a given dict. A list of tuples might be better here. – Mark Apr 07 '20 at 01:09

2 Answers2

0

How about this. althgouh im using a tuple instead of a dict.


data = [
    ("a",10),
    ("b",5),
("c", 7)
]

def get_lowest_index(input_list):
    list_of_values = [value[1] for value in input_list]
    return list_of_values.index(min(list_of_values))


while len(data)>0:
    lowest_index=get_lowest_index(data)
    print('Popping below item')
    print(data[lowest_index])
    data.pop(lowest_index)

Outupt:

Popping below item
('b', 5)
Popping below item
('c', 7)
Popping below item
('a', 10)
Emerson
  • 786
  • 3
  • 8
0

I would use pandas dataframes

import pandas as pd

# create your dictionary
mydict = {'a': 10, 'b': 5, 'c': 7}

# turn it into a pandas dataframe
df = pd.Series(mydict).to_frame('new_col').reset_index()
# give the columns names
df.columns = ['col1', 'col2']
# sort by the numerical column
df = df.sort_values(by=['col2'])
# loop through, obtaining objects in order of their associated value
for index, row in df.iterrows():
    print(row['col2'])
    my_object = row['col1']
5Ke
  • 924
  • 7
  • 27