Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > challenge ?

Reply
Thread Tools

challenge ?

 
 
alain
Guest
Posts: n/a
 
      03-22-2007
I have a problem I wonder if it has been solved before.
I have a dictionnary and I want the values in the dictionnary to be
annotated with the rank that would be obtained by sorting the values

def annotate_with_rank(my_dict):
....
return my_annotated_dict

In other words, any value a_value would become a 2-tuple
(a_value,rank_of_a_value)

I seek an elegant solution.

Alain

 
Reply With Quote
 
 
 
 
Frank Benkstein
Guest
Posts: n/a
 
      03-22-2007
Hi,

On 22 Mar 2007 09:41:43 -0700
"alain" <> wrote:

> I have a problem I wonder if it has been solved before.
> I have a dictionnary and I want the values in the dictionnary to be
> annotated with the rank that would be obtained by sorting the values
>
> def annotate_with_rank(my_dict):
> ....
> return my_annotated_dict
>
> In other words, any value a_value would become a 2-tuple
> (a_value,rank_of_a_value)
>
> I seek an elegant solution.


In your specification of the problem it is unclear what should be done
with duplicate values. My solution assigns every value a different
rank (starting from 0) such that the highest rank is len(my_dict) - 1.

def annotate_with_rank(my_dict):
items = my_dict.items()
items.sort(key = lambda (k, v): v)
return dict((k, (i, v)) for i, (k, v) in enumerate(items))

Best regards,
Frank Benkstein.


--
GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F
GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGArjXiPbWJb5jhm8RAgIaAJ9IQMzxVJtcG1hD9tKQKb dGPZgFTgCfT1VZ
RRLwnt6rrAI7HZ4mBSHRnjY=
=DSNB
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      03-22-2007
On Mar 22, 9:41 am, "alain" <alainpo...@yahoo.fr> wrote:
> I have a problem I wonder if it has been solved before.
> I have a dictionnary and I want the values in the dictionnary to be
> annotated with the rank that would be obtained by sorting the values
>
> def annotate_with_rank(my_dict):
> ....
> return my_annotated_dict
>
> In other words, any value a_value would become a 2-tuple
> (a_value,rank_of_a_value)


Try this:

>>> from operator import itemgetter
>>> my_dict = dict(a=10, b=5, c=8, d=12)
>>> for rank, (key, value) in enumerate(sorted(my_dict.items(), key=itemgetter(1))):

.... my_dict[key] = (value, rank)
>>> my_dict

{'a': (10, 2), 'c': (8, 1), 'b': (5, 0), 'd': (12, 3)}

 
Reply With Quote
 
Frank Benkstein
Guest
Posts: n/a
 
      03-22-2007
Hi, again,

On Thu, 22 Mar 2007 18:11:46 +0100
Frank Benkstein <frank-> wrote:

> On 22 Mar 2007 09:41:43 -0700
> "alain" <> wrote:
>
> > I have a problem I wonder if it has been solved before.
> > I have a dictionnary and I want the values in the dictionnary to be
> > annotated with the rank that would be obtained by sorting the values
> >
> > def annotate_with_rank(my_dict):
> > ....
> > return my_annotated_dict
> >
> > In other words, any value a_value would become a 2-tuple
> > (a_value,rank_of_a_value)
> >
> > I seek an elegant solution.

>
> In your specification of the problem it is unclear what should be done
> with duplicate values. My solution assigns every value a different
> rank (starting from 0) such that the highest rank is len(my_dict) - 1.


The two other possibilities were to still make len(my_dict) ranks but
assign equal values an equal rank. That would mean that some ranks are
untaken. Or, lastly, to make only as much ranks as there are unique
values.

> def annotate_with_rank(my_dict):
> items = my_dict.items()
> items.sort(key = lambda (k, v): v)
> return dict((k, (i, v)) for i, (k, v) in enumerate(items))


def annotate_with_rank_2(my_dict):
values = my_dict.values()
values.sort()
return dict((k, (values.index(v), v)) for k, v in my_dict.iteritems())

def annotate_with_rank_3(my_dict):
values = list(set(my_dict.itervalues()))
values.sort()
return dict((k, (values.index(v), v)) for k, v in my_dict.iteritems())

Best regards,
Frank Benkstein.

--
GPG (Mail): 7093 7A43 CC40 463A 5564 599B 88F6 D625 BE63 866F
GPG (XMPP): 2243 DBBA F234 7C5A 6D71 3983 9F28 4D03 7110 6D51

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFGArv3iPbWJb5jhm8RAi+DAJ4/5PpmXILgABgeI5Oe8q1oGGguQACfcrMF
zG+u3Cj/IhuFXpDlWjHKZiU=
=tGkI
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-22-2007
"alain" <> writes:
> def annotate_with_rank(my_dict):
> ....
> return my_annotated_dict
>
> In other words, any value a_value would become a 2-tuple
> (a_value,rank_of_a_value)
>
> I seek an elegant solution.


Untested:

def annotate_with_rank(my_dict):
s = sorted( ((v,i),k) for i,(k,v) in enumerate(my_dict.iteritems()))
return dict((k,v) for (v,k) in s)
 
Reply With Quote
 
Michael Spencer
Guest
Posts: n/a
 
      03-22-2007
alain wrote:
> I have a problem I wonder if it has been solved before.
> I have a dictionnary and I want the values in the dictionnary to be
> annotated with the rank that would be obtained by sorting the values
>
> def annotate_with_rank(my_dict):
> ....
> return my_annotated_dict
>
> In other words, any value a_value would become a 2-tuple
> (a_value,rank_of_a_value)
>
> I seek an elegant solution.
>
> Alain
>
>>> d = dict(a=10, b=5, c=8, d=12)


>>> dict((k, (v, rank)) for rank, (v, k) in

... enumerate(sorted((v, k) for k, v in d.items())))
{'a': (10, 2), 'c': (8, 1), 'b': (5, 0), 'd': (12, 3)}
>>>


# sort by value, then by key since (v,k) must be unique


Michael

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lab Challenge 2 www.networking-forum.com Cisco 1 05-26-2005 09:07 PM
Re:Routing Challenge Mitch Johnson Cisco 5 10-10-2004 09:42 PM
Routing Challenge Mitch Johnson Cisco 2 10-09-2004 10:39 AM
Parsing challenge... Artco News Perl 6 10-08-2003 02:59 PM
Parsing challenge... Artco News Perl 2 10-07-2003 08:03 PM



Advertisments