473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re: Fun with reverse sorts

On Thu, Oct 2, 2008 at 8:07 PM, David Di Biase <da**********@gmail.comwrote:
Hi there,

I'm sorting an expansive list descending according to a list of tuples.
Basically it has to sort the last value in the tuple (3) but if they are the
same then it should resort to using the second last value (2). Now according
to my very limited testing I've somewhat figured out that this SHOULD work:

list.sort(lambda a, b: (cmp(a[3], b[3]), cmp(a[2], b[2])) [a[3] == b[3]],
reverse = True)
Rather than defining a comparison function here (which is less
efficient), you can use the 'key' argument, which specifies a function
which is called for each item and returns a so-called key value that
the corresponding element should be sorted according to. Also, so your
slicing "[a[3] == b[3]]" isn't necessary because Python is smart and
sorts tuples that way anyway. Finally, be careful not to use "list" as
a variable name as this shadows the builtin 'list' type.

So the improved code is:

your_list.sort(key=lambda elem: (elem[3], elem[2]), reverse=True)
>
Here's an example of the list: [(34,23,54,34), (34,23,230,34),
(34,23,523,334), (34,23,15,17), (34,23,54,17), (45,23,43,123),
(564,23,543,23), (23,54,600,23), (34,54,23,654), (43,54,32,34)]

My first question is in regards to style first. The style guide for Python
doesn't seem to state this (if it has I missed it) but should I be doing
reverse=True or reverse = True with the spaces. lol this is so miniscule but
it's good to know. Also, does this function look/feel right to all the pros
out there. Is there a better way of doing it?
Actually, this is mentioned in PEP 8. You might not be familiar with
the term in use though ("keyword argument") which describes 'reverse'.
Here's the relevant section:

"""
- Don't use spaces around the '=' sign when used to indicate a
keyword argument or a default parameter value.

Yes:

def complex(real, imag=0.0):
return magic(r=real, i=imag)

No:

def complex(real, imag = 0.0):
return magic(r = real, i = imag)
"""

So you want the former: reverse=True

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
I'm still wrapping my head around ways of accomplishing proper sorts!

Dave

--
http://mail.python.org/mailman/listinfo/python-list

Oct 3 '08 #1
1 1429
Chris Rebert:
So the improved code is:
your_list.sort(key=lambda elem: (elem[3], elem[2]), reverse=True)
Better (untested):
from operator import itemgetter
....
your_list.sort(key=itemgetter(3, 2), reverse=True)

Bye,
bearophile
Oct 3 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
17
by: Sulu's Beard | last post by:
Hey gang, I'm evaluating the CA ERWin product for an upcoming data warehousing project. I'm most excited about the reverse engineering aspect of this system. I've sucessfully tested it on a...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
192
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
15
by: Fady Anwar | last post by:
Hi while browsing the net i noticed that there is sites publishing some software that claim that it can decompile .net applications i didn't bleave it in fact but after trying it i was surprised...
2
by: mdb | last post by:
I have a datagrid that is bound to a DataView, and it shows events and the times that event started ("StartTime") and stopped ("EndTime"). If the event has started but hasn't finished, then...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.