Blackasnight69
Blackasnight69

Reputation: 15

Python Finding the min and max of a dictionary without using the min max functions

So basically I have this Dictionary and I need to find the min and max without using the min and max functions and I need to return the key not the value. Below is what I currently have it returns the min and max value of the first key without looking at the rest and I have no idea how to make it return the Key instead any help is appreciated.

sol = {"Uranus":[2750, 3000, 2880], "Mercury":[46, 70, 57], "Earth":[147, 152, 150], "Venus":[107, 109, 108], "Mars":[205, 249, 228], "Saturn":[1350, 1510, 1430], "Jupiter":[741, 817, 779], "Pluto":[4440, 7380, 5910], "Neptune":[4450, 4550, 4500]}


def distance(sol, close=True):

    for name in sol:
        l = len(sol)
        closefar = sol[name][0]
        for i in range(1, l):
            if close:
                if sol[name][i] < closefar: closefar = sol[name][i]
            else:
                if sol[name][i] > closefar: closefar = sol[name][i]
            return closefar

print("Min:", distance(sol, True))
print("Max:", distance(sol, False))

Upvotes: 0

Views: 1449

Answers (3)

andreis11
andreis11

Reputation: 1141

Tested this approach by changing the values from inside the list and changing the position of modified values.

sorted(sol.items(), key=lambda e: sorted(e[1], reverse = True)[len(e)])[0][0]
Mercury

Biggest:

sorted(sol.items(), key=lambda e: sorted(e[1])[len(e)], reverse = True)[0][0]
Pluto

Explanations:

sorted(sol.items()) returns a list of tuple pairs (alphabetical, Earth, Jupiter, Mars and so on)

Therefore, we use another key (sorting criteria):

 key=lambda e: sorted(e[1])[2])
 Tuple pairs:
 e[0] = Uranus, Mercury and so on                     (dict keys)
 e[1] = [2750, 3000, 2880], [46, 70, 57] and so on    (dict values)

We are applying another sort to the tuple values (e[1]):

 sorted(e[1]) 

translates to:

 sorted([46, 70, 57]) = [46, 57, 70]

And here we capture the biggest value per planet:

 sorted([46, 70, 57])[2] = [46, 57, 70][2] = [70] Mercury
 sorted([147, 152, 150])[2] = 152 Earth

 [('Mercury', [70]), ('Venus', [109]), ('Earth', [152]), ('Mars', [249]), ('Jupiter', [817]) ...]

To take the min value of each list:

 sorted(e[1], reverse = True)

Again, as mentioned at the start of this post, test by changing the values of your list and change the position of the biggest/min value.

Hope it helps.

Upvotes: 1

Andrea Rodr&#237;guez
Andrea Rodr&#237;guez

Reputation: 11

Well, this might be more than what you need, but I'll show you an alternative:

Turn your dictionary into a list of tuples. The [0] part of the tuple will have the key, [1] will have the value (a list, in your case)

list_sol = list(sol.items())

Sort the internal lists, you'll see why afterwards

for x in list_sol:
    x[1].sort()

Here's the exciting part

list_sol.sort(key = lambda x: x[1][0])

What does this do? Well, we are sorting the list of tuples. How? To sort, we are looking at the value x[1][0]. This value is the first number of each list (that comes as the value of each dictionary entry). We sorted these lists earlier to make sure the first element is the smallest number and the last element, the largest.

Now, to get the key of the minimum distance, we just need to grab the first element of the list of tuples, and then grab the string corresponding to the key of the dictionary.

list_sol[0][0] # here's Mercury!

Now, the greatest distance is at list_sol[-1]? Nope. Remember, we sorted this list looking at the first element of each list of distances, which is the shortest one. We have to do the same with the largest one. Here's how:

list_sol.sort(key = lambda x: x[1][-1])

Just to clarify, x[1] picks the value of the dictionary, x[1][-1] picks the last element of the value (because it's a list).

Finally,

list_sol[-1][0] # here's Pluto!

Upvotes: 1

bashBedlam
bashBedlam

Reputation: 1500

You need an inner loop to look at each element of each dictionary entry and compare it with all other elements from all of the other entries.

sol = {"Uranus":[2750, 3000, 2880], "Mercury":[46, 70, 57], "Earth":[147, 152, 150], "Venus":[107, 109, 108], "Mars":[205, 249, 228], "Saturn":[1350, 1510, 1430], "Jupiter":[741, 817, 779], "Pluto":[4440, 7380, 5910], "Neptune":[4450, 4550, 4500]}

def distance(close=True):
    closest = 999999 
    farthest = 0
    for name in sol:
        for index in range (3) :
            closefar = sol[name][index]
            if close :
                if closefar < closest :
                    closefarname = name
                    closest = closefar
            else :
                if closefar > farthest :
                    closefarname = name
                    farthest = closefar
    return closefarname

print("Min:", distance(True))
print("Max:", distance(False))

Upvotes: 1

Related Questions