How to return the highest float value in a dictionary?

write a python program to get the maximum and minimum value in a dictionary
python find max value in list of dictionaries
python find max value in dictionary of dictionaries
get key with highest value dictionary python
max of values in a dictionary
how to print the largest value in a dictionary

Below is my codes that I have tried, the check_keyword() method is to basically compare a string with a dictionary of words, if words matched, increment count and find the highest value in the dictionary:

Please focus on the codes where I have commented "find the maximum float value"

def check_keyword():
    new_dict = {}
    count_dict = {}
    new_list = []
    new_list2 = []
    count = 0
    with open(unknown.txt, "r") as fp:
        unknown_file = fp.read()
        print(unknown_file)
        # read key phases from text file as a dictionary
    df = pd.read_csv(key_phases.txt, sep='|')
    key_phases_dict = df.to_dict(orient='records')

    for i in key_phases_dict:
        new_list = list(i.values())
        new_dict[new_list[0]] = new_list[1]

    for key in new_dict.keys():
        count_dict[key] = 0
        new_list2 = new_dict[key].split(",")
        new_dict[key] = new_list2
        for j in new_dict[key]:
            if j in unknown_file:
                print(j)
                count_dict[key] = count_dict[key] + 1
        count_dict[key] = float(count_dict[key] / len(new_list2))
    print(count_dict)
    # find the maximum float value 
    for k, v in count_dict.items():
        if v > count:
            highest_list = []
            result = k, v
            highest_list.append(result)
            count = v
        else:
            v == count
            result = k, v
            highest_list.append(result)

    return highest_list

The output of count_dic:

{2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}

The problem encountered is that when I print highest_list it gives me (it does not shows me the highest value):

[(3, 0.08666666666666667), (4, 0.08666666666666667), (5, 0.0), (6, 0.04666666666666667), (7, 0.02), (8, 0.013333333333333334)]

Desired output to achieve:

[(3, 0.08666666666666667),(4, 0.08666666666666667)]

You can just calculate the maximum value and then use a list comprehension:

d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}

maxval = max(d.values())
res = [(k, v) for k, v in d.items() if v == maxval]

[(3, 0.08666666666666667), (4, 0.08666666666666667)]

Python: Get the maximum and minimum value in a dictionary , Python dictionary: Exercise-15 with Solution. Write a Python program to get the maximum and minimum value in a dictionary. Returns the symmetric difference between two lists, after applying the provided function to each  In this tutorial, learn how to get key with a maximum value in Dictionary using Python. The short answer is to use the Python max() function to find a key with a maximum value in Dictionary. Each key of the Dictionary may contain the elements with the numeric values. The elements are not arranged in sequence to find the maximum value.

Here's two ways to go about it.

One with sorted and a list comprehension:

d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}

sorted_items = sorted(d.items(), key=lambda x: x[1], reverse=True)
results = [item for item in sorted_items if item[1] == sorted_items[0][1]]

# output: [(3, 0.08666666666666667), (4, 0.08666666666666667)] #

And the other with sorted and filter:

d = {2: 0.02666666666666667, 3: 0.08666666666666667, 4: 0.08666666666666667, 5: 0.0, 6: 0.04666666666666667, 7: 0.02, 8: 0.013333333333333334}

sorted_items = sorted(d.items(), key=lambda x: x[1], reverse=True)
results = filter(lambda x: x[1] == sorted_items[0][1], sorted_items)

# output: [(3, 0.08666666666666667), (4, 0.08666666666666667)] #

With sorted you can use key to sort the items by the dictionary's values. sorted_items will give you:

[(3, 0.08666666666666667), (4, 0.08666666666666667), (6, 0.04666666666666667), (2, 0.02666666666666667), (7, 0.02), (8, 0.013333333333333334), (5, 0.0)]

Including reverse makes it so the first index of the results will be the highest value.

The 2nd line to get results is to filter the list if there are multiple indexes that have the same max value. With that it trims the list, and you end up with the final two values.

How to Get the Key with Maximum Value in a Python Dictionary , You can use it to find the maximum value of any iterable or any  Python dictionary: Exercise-15 with Solution. Write a Python program to get the maximum and minimum value in a dictionary. Sample Solution:- . Python Code:

Instead of

v == count
result = k, v
highest_list.append(result)

Try:

v = count
result = k, v
highest_list.append(result)

In other words, change == to =.

Find The Max Value In A Dictionary, Find The Max Value In A Dictionary. 20 Dec 2017. Create A Dictionary. ages = {'​John': 21, 'Mike': 52, 'Sarah': 12, 'Bob': 43 }. Find The Maximum Value Of The  Find keys by value in dictionary As, dict.items() returns an iterable sequence of all key value pairs in dictionary. So, we will iterate over this sequence and for each entry we will check if value is same as given value then we will add the key in a separate list i.e.

Python : How to get all keys with maximum value in a Dictionary , Now in the above example to find the key with max value in dictionary. We passed the Iterable returns by dict.items(), that is basically an Iterable  A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Programming Collective Intelligence: Building Smart Web 2.0 , All the scoring methods introduced here return dictionaries of the URL IDs and minscore=min(scores.values()) return dict([(u,float(minscore)/max(vsmall,l)) for  The view object values doesn't itself return a list of sales item values but it returns a view of all values of the dictionary. If the list is updated at any time, the changes are reflected on to the view object itself, as shown in the above program.

Data Structures (Part II): Dictionaries, Python's dictionary allows you to store key-value pairs, and then pass the dictionary a key the folly of using a floating-point number as a key # in a dictionary >>> x = {} >>> x[(0.1 + Return a tuple of all of the keys that map to that max value. Python List max() Method - Python list method max returns the elements from the list with maximum value.

Comments
  • Could you kindly explain the code snippets to me... I dont really get the sort and filter codes? @Green call
  • @School I added some more explanations, hope that helps. If you are stumped on lambda, map, or filter try googling examples of them. Not trying to dodge the answer there, it's just that the top results will explain them better than I could.
  • Thanks @Green Cell I roughly get it, I will look up the lambda, map and filter for more understanding, thanks!