473,516 Members | 2,910 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1850
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2238
by: Robert Ferrell | last post by:
I'm wondering how Python cognoscenti use/implement factory patterns in Python. In my limited C++ experience, one reason to use a factory method is to make it easier to create instances of classes derived off a base class. The instance is declared to be an instance of the base class. The factory returns an instance of a derived class, and...
226
12320
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
30
3383
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
114
9693
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
10
3663
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
20
4024
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
16
7154
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain unchanged). How can I do that in Python? Claudio Grondi
206
8172
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of...
70
2580
by: TheFlyingDutchman | last post by:
Python user and advocate Bruce Eckel is disappointed with the additions (or lack of additions) in Python 3: http://www.artima.com/weblogs/viewpost.jsp?thread=214112
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.