[CentralOH] Python reversed() Question

Fandi Peng fandi.814 at gmail.com
Fri Apr 20 03:46:17 CEST 2012


On Thu, Apr 19, 2012 at 9:16 PM, Kris Hardy <kris at rkrishardy.com> wrote:
> sorted([("D3","E4"), ("A3","B2"),("A2","C4")...], lambda item: item[1])

It looks very simple but I got an error message
"TypeError: must use keyword argument for key function"
when running the above line for python 3.2 and
"TypeError: <lambda>() takes exactly 1 argument (2 given)"
for python 2.7



> On 4/19/2012 7:12 PM, Fandi Peng wrote:
>>
>> Hi guys, I have a question:
>> Suppose I have a list, every element in the list is a tuple,
>> every tuple has two elements,
>> e.g. [("D3","E4"), ("A3", "B2"), ("A2","C4")....]
>> Therefore, if I sorted the list, the tuples would be rearranged
>> based on the first element of each tuple:
>> e.g. [("A2","C4"), ("A3", "B2"), ("D3","E4")....]
>> Now I want this list to be sorted based on the second element
>> of each tuple:
>> e.g. [("A3", "B2"), ("A2","C4"), ("D3","E4"),....]
>> What's the simplest code could you come up to achieve
>> this?
>> _______________________________________________
>> CentralOH mailing list
>> CentralOH at python.org
>> http://mail.python.org/mailman/listinfo/centraloh
>
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh


More information about the CentralOH mailing list