<div dir="ltr">I did see that actually, I thought it was only applied to specifying default parameters and wasn't sure if it ALSO applied to putting it into a function. In a way however, I see what you're getting at - it's basically the same thing you're just specifying a default value the same way...<br>
<br>Ok problem resolved.<br><br>Grazie tanto ;-)<br><br>David<br><br><div class="gmail_quote">On Fri, Oct 3, 2008 at 12:08 AM, Chris Rebert <span dir="ltr"><<a href="mailto:clp@rebertia.com">clp@rebertia.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">On Thu, Oct 2, 2008 at 8:07 PM, David Di Biase <<a href="mailto:dave.dibiase@gmail.com">dave.dibiase@gmail.com</a>> wrote:<br>
> Hi there,<br>
><br>
> I'm sorting an expansive list descending according to a list of tuples.<br>
> Basically it has to sort the last value in the tuple (3) but if they are the<br>
> same then it should resort to using the second last value (2). Now according<br>
> to my very limited testing I've somewhat figured out that this SHOULD work:<br>
><br>
> list.sort(lambda a, b: (cmp(a[3], b[3]), cmp(a[2], b[2])) [a[3] == b[3]],<br>
> reverse = True)<br>
<br>
</div>Rather than defining a comparison function here (which is less<br>
efficient), you can use the 'key' argument, which specifies a function<br>
which is called for each item and returns a so-called key value that<br>
the corresponding element should be sorted according to. Also, so your<br>
slicing "[a[3] == b[3]]" isn't necessary because Python is smart and<br>
sorts tuples that way anyway. Finally, be careful not to use "list" as<br>
a variable name as this shadows the builtin 'list' type.<br>
<br>
So the improved code is:<br>
<br>
your_list.sort(key=lambda elem: (elem[3], elem[2]), reverse=True)<br>
<div class="Ih2E3d"><br>
><br>
> Here's an example of the list: [(34,23,54,34), (34,23,230,34),<br>
> (34,23,523,334), (34,23,15,17), (34,23,54,17), (45,23,43,123),<br>
> (564,23,543,23), (23,54,600,23), (34,54,23,654), (43,54,32,34)]<br>
><br>
> My first question is in regards to style first. The style guide for Python<br>
> doesn't seem to state this (if it has I missed it) but should I be doing<br>
> reverse=True or reverse = True with the spaces. lol this is so miniscule but<br>
> it's good to know. Also, does this function look/feel right to all the pros<br>
> out there. Is there a better way of doing it?<br>
<br>
</div>Actually, this is mentioned in PEP 8. You might not be familiar with<br>
the term in use though ("keyword argument") which describes 'reverse'.<br>
Here's the relevant section:<br>
<br>
"""<br>
- Don't use spaces around the '=' sign when used to indicate a<br>
keyword argument or a default parameter value.<br>
<br>
Yes:<br>
<br>
def complex(real, imag=0.0):<br>
return magic(r=real, i=imag)<br>
<br>
No:<br>
<br>
def complex(real, imag = 0.0):<br>
return magic(r = real, i = imag)<br>
"""<br>
<br>
So you want the former: reverse=True<br>
<br>
Cheers,<br>
Chris<br>
--<br>
Follow the path of the Iguana...<br>
<a href="http://rebertia.com" target="_blank">http://rebertia.com</a><br>
<div class="Ih2E3d"><br>
><br>
> I'm still wrapping my head around ways of accomplishing proper sorts!<br>
><br>
> Dave<br>
><br>
</div>> --<br>
> <a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
><br>
><br>
</blockquote></div><br></div>