Jump to content
Sign in to follow this  
mymind

ordenação de dicionarios

Recommended Posts

mymind

Boas

uma vez mais trago um novo pedido de ajuda ....

"""

um programa que mostre ao utilizador os elementos de um dicionário

(chave,valor) ordenados pelo valor.

Exemplo:

Input={âaâ: 1,âbâ: 7,âcâ: 3,âdâ: -2,âeâ: 5,âfâ: -6,âgâ: 3.5}

Output:[(âfâ, -6), (âdâ, -2), (âaâ, 1), (âcâ, 3), (âgâ, 3.5), (âeâ, 5), (âbâ, 7)].

"""

def ordena_valor():

    Input={'a':1,'b':7,'c':3,'d':-2,'e': 5,'f':-6,'g': 3.5}

    new_input=Input.copy()

    #print new_input

    Input=sorted(Input.values())

    #print Input

    resultado= {}

   

    for i in Input:

        for item in new_input:

            if item==i:

                resultado[item]=i

    print resultado

           

alguem me pode dar umas dicas de como por isto a rolar...

tank s 🤔 :D

Share this post


Link to post
Share on other sites
mymind

Boas

aki esta a solução: :wallbash:

from operator import itemgetter

def ordena_valor():

    Input={'a':1,'b':7,'c':3,'d':-2,'e': 5,'f':-6,'g': 3.5}

    new_input=Input.items()

    print new_input

    input2=sorted(new_input,key=itemgetter(1))

    print input2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.