mymind Posted January 3, 2010 at 08:07 PM Report Share #303914 Posted January 3, 2010 at 08:07 PM 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 ? 😄 Link to comment Share on other sites More sharing options...
mymind Posted January 5, 2010 at 01:43 PM Author Report Share #304211 Posted January 5, 2010 at 01:43 PM Boas aki esta a solução: 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now