sorting the list by inner elements

Scott David Daniels Scott.Daniels at Acm.Org
Sat Mar 27 11:28:05 EST 2004


Josiah Carlson wrote:

>> ...i heard use of lambda function is not good !. So why it is not good,...
> There is nothing explicitly wrong with using lambda.  However, unless 
> the lambda you are using is easy to understand, ....
> def cmp_1(x,y):
>     return cmp(x[1], y[1])

But, of course, the reason to avoid lambda is that you avoid a place to
make your code clearer.  A much better choice in this case is:

     def compare_cities(name_city1, name_city2):
         return cmp(name_city1[1], name_city2[1])

     name_city_sort(compare_cities)

Of course I believe a coming feature will eventually do what you want in
a completely different way:

     def getcity(name_city):
         return name_city[1]

     name_city_list.sort(key=getcity)

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list