简体   繁体   中英

How to return the highest float 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)]

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.

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 = .

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