简体   繁体   中英

Selecting the maximum value for each key in a dictionary python

For my code I need to sort lots of lines of data (from a.csv file) that contain the keys and values. However, for each key there are many values. I need to form code that can loop and form a dictionary where it prints the keys with only the highest value for each particular key.

dict = {}

for i in dict:
    dict.update({i: 0})

for line in data:
    dict = line.split(",")
    value = int(data[14]) 
    key = int(data[3])
    dict.update({key: value})

print(maximum_population)

The above code prints all the data, I need to add code that means it only prints the highest value for each key

ie

{key: highest_value}

As others mentioned, you should post a sample of your data. The way you're looping over it right now doesn't make much sense - you are creating a list with dict = line.split(",") and then you aren't using it.

To improve on another answer, there is no need to sort any values. Use max() to keep this operation linear time.

data_dict = {"k1": [1,2,3,4], "k2": [101, 2, 43], ...}
result = {key: max(values) for key, values in data_dict.items()}

If you want to print the results:

for k, v in result.items():
    print(k, v)

try the below code data is a dictionary with key and list of values

e.g :: data = {1:[1,2,3,4],2:[8,7,6]}

def maximum_population(data):
    result={}
    for k,v in data.items():
        v.sort(reverse=True)
        result[k] = v[0]
    return result 

its unclear from your question if you read the key on a line and multiple values on the same line. or if you mean you read only a single key and single value from each line and that the key may appear again on other lines and you only want to map the key to the highest of the value seen on any of the multiple lines the key appears on.

based on your comment i assume a single key and value on each line but that key might appear in another line. IE each line doesn't have a unique key and you want the highest value for the key.

For the purpose of this i have just zipped the data together into pairs like they were on the same line. The principle is the same, if we have never seen this key then store it with the value, otherwise if we have seen this key and this value is higher than the value we seen before, update the value for this key

keys = "5 6 6 3 3 6 6 4 2 2 5 7 7 8 8 6 3 6 4 3 9 15 13"
values = "232315 23 231723 25 232389 19 231819 232368 30 232370 231864 16 230150 14 230191 230688 228461 227789 226375 226928 225213 225174 223570"
data = zip((int(key) for key in keys.split()), (int(value) for value in values.split()))
my_highest_values = {}

for key, value in data:
    print("#", key, value)
    if key in my_highest_values:
        if value > my_highest_values[key]:
            my_highest_values[key] = value
    else:
        my_highest_values[key] = value

print(my_highest_values)

OUTPUT

{5: 232315, 6: 231819, 3: 232389, 4: 232368, 2: 232370, 7: 230150, 8: 230191, 9: 225213, 15: 225174, 13: 223570}

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