Is Python Smart Enough to do Sorting like this?

Ivo Spam at ivonet.nl
Sat Mar 13 04:13:45 EST 2004


Do a google for DiveIntoPython and read the first chapters. it will blow
your mind on string manipulation.

Cheerz,
Ivo.
http://IvoNet.nl
"Life is like a nose.. You have to get out of it what's in it!"


"shalendra chhabra" <shalen_itbhu at hotmail.com> wrote in message
news:mailman.342.1079151943.19534.python-list at python.org...
>
> Hey Stephen,
> Thanks. I got what you said and by mistake I had typed the order in
> Decreasing, which I needed too.
>
> However, right now I see this interesting problem here on this list of
"dont
> bother" which seems interesting.
>
> If one has a string and can this string be assigned to a dictionary and
then
> we can apply the sorting methods of dictionary on it.
> For example: the information in my itemset can be treated as a string.
>
> itemset{}
> itemset=str(key[i])+str(value[i]) #done over a loop and and then some how
> can get everything concatenated
>
> If I am not mistaken the itemset is now a string.
> How can I assign this itemset back to a dictionary? Is there any way?
> If yes! then we can apply the sort methods for increasing order and for
> decreasing order you have given me the function already.
>
> Please clear my doubt
>
> Thanks
> Shalen
>
> >From: Stephen Horne <steve at ninereeds.fsnet.co.uk>
> >To: python-list at python.org
> >Subject: Re: Is Python Smart Enough to do Sorting like this?
> >Date: Sat, 13 Mar 2004 04:03:30 +0000
> >
> >On Sat, 13 Mar 2004 01:26:25 +0000, "shalendra chhabra"
> ><shalen_itbhu at hotmail.com> wrote:
> >
> > >Is it possible to order this itemset in an increasing order of key:
value
> > >with respect to keys. For example: if
> > >key4>key2>key3>key1
> > >then the resulting ordering should be in such a way:
> > >
> > >key4:value4, key2: value2,key3:value3>key1: value1
> >
> >What you seem to be describing is decreasing order of key - the
> >highest key value (key4) first, with progressively smaller values at
> >later positions.
> >
> >This is not hard. The only minor annoyance is that, unlike for
> >instance a C++ map, Python dictionaries don't naturally iterate in
> >sorted order (they use hashing rather than a tree structure, which
> >overall is probably neither better nor worse - just different).
> >
> >Sorting tasks are quite common, so while that are not exactly
> >difficult I imagine there's a good guide on the internet somewhere for
> >newbies (links anyone?).
> >
> >In this case, once you have the dictionary of key:value pairs you
> >extract a list of keys and sort it appropriately. The default sort is
> >in increasing order, so you reverse that to get decreasing order.
> >Then, if necessary, you use a list comprehension to get a list of
> >(key, value) tuples in the needed order. The list comprehension is
> >often redundant, though, as you can normally just iterate the sorted
> >keys and read the values from the dictionary when they're needed.
> >
> >Here is a quick function...
> >
> >def pairs_by_decreasing_key (p_Dict) :
> >   """
> >   Takes a dictionary and derives a list of (key, value) tuples
> >   in decreasing order of key.
> >   """
> >   tmp = p_Dict.keys (); tmp.sort (); tmp.reverse ()
> >
> >   return [(i, p_Dict [i]) for i in tmp]
> >
> >
> >--
> >Steve Horne
> >
> >steve at ninereeds dot fsnet dot co dot uk
> >--
> >http://mail.python.org/mailman/listinfo/python-list
>
> _________________________________________________________________
> Xerox Multifunction devices that print,copy,scan and fax.
> http://go.msnserver.com/IN/44797.asp Get affordable printing solutions
that
> fit your needs .
>
>





More information about the Python-list mailing list