Finding the largest key in a Python dictionary

General:

I need help finding a path in python to get max N elements in a python multidimensional dictionary. For instance:

  things = { "car": { "weight": 100 }, "apple": { "weight": 1 }, "spanner": { "weight": 10 } } 

In this case, I would like to find the 2 highest values ​​in the dictionary, in particular the keys of these elements. Therefore, in this case, he must return ["car", "spanner"]

Actual problem:

Note. This is my first attempt at a genetic algorithm, so I may not be doing it right. For everyone.

Since I am English, I am looking for the best cup of tea I can imagine, so I write a python program that generates 10 random cups of tea and then uses natural selection to find the top five in the top ten and so on.

A cup of tea is modeled as a python dictionary with 5 keys:

 { "brew_time": Some Number, "milk": Some Number, "sweeteners": Some Number, "fitness": Some Number (This is what I'm interested in), "name": Some randomly generated name (Doesn't really matter) } 

The cup of tea that my program spits out will look something like this:

 {'brew_time': 2.0, 'milk': 0.5, 'sweeteners': 3.0, 'name': 'bold cup', 'fitness': 0} 

Then it generates 10 cups of tea stored in the teas variable. This is an example of the output of this:

 {0: {'brew_time': 2.0, 'milk': 0.4, 'sweeteners': 1.0, 'name': 'unafraid brew', 'fitness': 0}, 1: {'brew_time': 3.0, 'milk': 0.5, 'sweeteners': 3.0, 'name': 'fire-eating blend', 'fitness': 0}, 2: {'brew_time': 2.0, 'milk': 0.6, 'sweeteners': 2.0, 'name': 'fearless drink', 'fitness': 0}, 3: {'brew_time': 2.0, 'milk': 0.9, 'sweeteners': 3.0, 'name': 'fire-eating blend', 'fitness': 0}, 4: {'brew_time': 2.0, 'milk': 0.8, 'sweeteners': 2.0, 'name': 'fire-eating cuppa', 'fitness': 0}, 5: {'brew_time': 3.0, 'milk': 0.3, 'sweeteners': 1.0, 'name': 'fire-eating drink', 'fitness': 0}, 6: {'brew_time': 4.0, 'milk': 0.7, 'sweeteners': 2.0, 'name': 'dauntless medley', 'fitness': 0}, 7: {'brew_time': 3.0, 'milk': 0.3, 'sweeteners': 2.0, 'name': 'dauntless cuppa', 'fitness': 0}, 8: {'brew_time': 3.0, 'milk': 0.9, 'sweeteners': 2.0, 'name': 'epic drink', 'fitness': 0}, 9: {'brew_time': 2.0, 'milk': 0.4, 'sweeteners': 2.0, 'name': 'gusty drink', 'fitness': 0}} 

Now I'm trying to code a function called selection() that will remove the 5 least suitable teas from the dictionary. (I installed the fitness chat using the rank_tea() function, which takes an array and installs all tea accessories, which is a number from 0 to 1, which represents the quality of the tea).

This is what I have so far, but it does not work:

 def selection(): teaCopy = teas.copy() fitnesses = [] for i in range(0, len(teaCopy)): fitnesses.append(teas[i]["fitness"]) print(fitnesses) max_fitnesses_indicies = sorted(range(len(fitnesses)), key=lambda x: fitnesses[x]) print(max_fitnesses_indicies) len_array = [] print(len_array) for i in range(0, len(teas)): len_array.append(i) to_be_del = list( set(max_fitnesses_indicies) - set(len_array) ) print(to_be_del) 

This is the complete code. Sorry for the length of the question, I just didn't want to miss anything.

Any help would be appreciated

+5
source share
1 answer

You can simply use:

 >>> sorted(things.keys(),key=lambda x:things[x]['weight'],reverse=True) ['car', 'spanner', 'apple'] 

Get a list of items sorted by their weight (here in reverse order to sort heavier things first). Therefore, if you call:

 >>> sorted(things.keys(),key=lambda x:things[x]['weight'],reverse=True)[:2] ['car', 'spanner'] 

you will get two of the hardest. But this will work in O (n log n) . In case the number of k values ​​you want to get is small (compared to the total number). You can use heapq :

 from heapq import nlargest result = nlargest(k,things.keys(),key=lambda x:things[x]['weight']) 

which, as far as I know, will be < launched in O (n log k) (k are the numbers of the elements you want to select).

+2
source

Source: https://habr.com/ru/post/1264472/


All Articles