Is Python Smart Enough to do Sorting like this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shalendra chhabra

    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:val ue3>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:val ue4}

    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!


  • Stephen Horne

    #2
    Re: Is Python Smart Enough to do Sorting like this?

    On Sat, 13 Mar 2004 01:26:25 +0000, "shalendra chhabra"
    <shalen_itbhu@h otmail.com> wrote:
    [color=blue]
    >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:val ue3>key1: value1[/color]

    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_decrea sing_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

    Comment

    Working...