Python - Higher value of a dictionary per key

Asked

Viewed 2,357 times

0

Good morning to all.

I need a lot of help with a project I’m doing. I’m having trouble getting only the highest values from a dictionary or a list. I’ll try to explain it better:

In AWK language I would do so for the highest values:

if (FILENAME == "..\\temp\\empregados.txt") {
    emp = substr($1, 1, 3)+0;
    i_emp = substr($1, 5)+0;

    if (i_emp > maior[emp])
        maior[emp] = i_emp

    print $0 > "..\\dados\\empregados.txt"

Here "if (i_emp > major[emp])" is the cat jump, because I create a arrey called "major and step as position of that arrey emp. With this he will tell me of the companies I have registered, which is the code of the largest employee per company.

I haven’t seen how to work with arreys of this type in Python so I can get the biggest code of each company.

Ex: I have csv file separated by "tab" asssim:

codigo_empresa codigo_empregado 2 2-201 2 2-200 2 2-305 2 2-002 3 3-305 3 3-405 3 3-105

And so it goes. I need him to keep the following values: 2 2-305 3 3-405

From now on I thank you all for your subsequent help.

  • Questions are for... well, questions. If you want to answer your own question, publish a response. Just be careful not to answer only with the code that is working; along with the post code the explanation of what was wrong and how it solved.

  • Thanks for the clarification Anderson Carlos Woss. I’m new here, so I’m not used to working, but I’m adjusting.. Grateful.

  • Look at this link

4 answers

0


That’s the way I found to solve my problem:

for row in read_file:
    codi = dict(row).get("CODI")
    codigo_empresa = codi[:3]
    codigo_empregado = codi[4:8]

   if codigo_empresa not in dict_empregado_maior:
       dict_empregado_maior[codigo_empresa] = codigo_empregado
   elif codigo_empregado > dict_empregado_maior[codigo_empresa]:
       dict_empregado_maior[codigo_empresa] = codigo_empregado

0

You can use the sorted with the parameter reverse=True if you want a list in descending order.

lista = 1.2, 2.8, 3.9, 4.1, 5.8

print(sorted(lista, reverse=True))

Exit:

[5.8, 4.1, 3.9, 2.8, 1.2]
  • Good morning Éder Garcia. This would even be the solution if I wanted to order only, but I want to leave only the larger codes of each company. I’m studying a solution with dictionaries, if it works here the result.

  • Good luck Cleber. Waiting to see the solution!

0

If I understand correctly. you have a dictionary of the type below:

empr = {2:[201,200,305,2],
        3:[305,405,105]
        }

So one way to solve this is this:

selec = {}

for key in empr.keys(): # não precisa do .keys() mas fica mais intuitivo
    selec[key] = max(empr[key])


print(selec)

{2: 305, 3: 405}

Hit iterate by the key the dic generated by the table and get the maximum value.

  • Thanks Julio, this is a better way to write what I did up there. Thanks.

0

Good morning you can use Python’s MAX function .

dic = {'A': 58, 'B': 100, 'C': 42, 'D': 99, 'E': 12}
dic[max(dic, key=dic.get)]

Exit:

100
  • Thank you very much Johnatan Dantas, but it will also bring me only the highest general value of the dictionary and not the highest value of each key. I’ve done what I wanted and I’ll post it here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.