Python Max Value Lists

Asked

Viewed 127 times

-2

I have a question about the lists in python. Suppose I have one lista1, with name, store and sales value, I have to return the name of the person who sold the most. example:

vendedores= [{"nome": "Maria", "loja": 1, "valor": 100.00},
             {"nome": "João", "loja": 2, "valor": 110.00},
            ]

How to relate the highest value to the seller’s name and return only the name of the person who sold the most?

Thank you, from now on!

  • 1

    In fact you have a list of dictionaries, in this code, study the lists and also the dictionaries. :-)

3 answers

2

The very function max Python native allows you to define which will be the rule to define which is the highest value item in your iterable through the parameter key. In your case, just find the item that has the largest field valor, thus:

vendeu_mais = max(vendedores, key=lambda vendedor: vendedor['valor'])

print(vendeu_mais['nome'])
  • This answer that should be marked as correct.

  • @Augustovasques Must be duplicate, I just haven’t found it yet

  • This one https://answall.com/questions/367701/lista-max-min-em-python?rq=1 ?

0


valor,nome=float(0),""


 for i in vendedores:
       if i['valor']>valor:
          valor=i['valor']
          nome=i['nome']
print("vendedor {} :\nvalor: {}".format(nome,valor))

-1

for i in vendedores:
       if i['valor']>valor:
          valor=i['valor']
          nome=i['nome']
print("vendedor {} :\nvalor: {}".format(nome,valor))
  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • @Tiedttech, when you do the review, pay more attention to the messages you link with this analysis. This message is intended for the user who responds only with links. My impression on the answer is that even though it is weak it is still a concrete attempt to solve the problem and I (personally, you or another user may disagree) understand it as a valid response attempt.

Browser other questions tagged

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