469,898 Members | 1,710 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Is Python Smart Enough to do Sorting like this?

Hi,
I just had a tryst with python.
I was wondering if python is good enough to do this kind of job -- for it
has extensive support of string and pattern matching, ordering and list
handling.

There are 'n' itemsets, the size of n is unknown. The program can count
itself while scanning. Each of the n itemset is of the form key: value

{key1: value1, key2:value2, key3, value3, key4: value4 .....}

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

I plan to do this by storing this whole set of itemset in a vector or map.

Lets say vector v={key1:value1, key2: value2, key3: value3,key4:value4}

Now I wish to apply the sorting over this vector v, such that I have the
entries in the increasing order with respect to keys in v again.
Can some one point to me how to achieve this with python?
A snippet.....

Thanks,

Shalen

__________________________________________________ _______________
Protect your PC from viruses. Get in the experts.
http://www.msn.co.in/pcsafety/ Click here now!
Jul 18 '05 #1
1 1695
On Sat, 13 Mar 2004 01:26:25 +0000, "shalendra chhabra"
<sh**********@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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Robert Ferrell | last post: by
226 posts views Thread by Stephen C. Waterbury | last post: by
114 posts views Thread by Maurice LING | last post: by
10 posts views Thread by Andrew Dalke | last post: by
20 posts views Thread by Xah Lee | last post: by
16 posts views Thread by Claudio Grondi | last post: by
70 posts views Thread by TheFlyingDutchman | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.