Re: Case-Insensitive Sorting of Multi-Dimensional Lists
Joe [EMAIL PROTECTED] wrote: I am new to lambda and have searched for a few hours this morning, coming up empty handed. Is this possible? Seeing as it has happened, it must be. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
Ben Finney wrote: mosscliffe [EMAIL PROTECTED] writes: I have tried the following, for a one dimensional list and it works, but I can not get my head around this lambda. How would this be written, without the lamda ? mylist = ['Fred','bill','PAUL','albert'] mylist.sort(key=lambda el: el.lower()) Here's a hint: FoO.lower() 'foo' str.lower(FoO) 'foo' But be aware that you lose both the ability to override and duck-typing: print r # derived from str Upper East Side print s # unicode München r.lower(), str.lower(r) ('Lower East Side', 'upper east side') s.lower(), str.lower(s) Traceback (most recent call last): File stdin, line 1, in module TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode' Peter -- http://mail.python.org/mailman/listinfo/python-list
Case-Insensitive Sorting of Multi-Dimensional Lists
I have a list of lists that I would like to sort utilizing a certain index of the nested list. I am able to successfully use: Import operator list = [[Apple, 1], [airplane, 2]] list.sort(key=operator.itemgetter(0)) But, unfortunately, this will be case sensitive (Apple will come before airplane because the A is capital) and I need it to be insensitive. Can someone provide some input, please? Thanks in advance! Jough -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
On 6/7/07, Joe [EMAIL PROTECTED] wrote: I have a list of lists that I would like to sort utilizing a certain index of the nested list. I am able to successfully use: Import operator list = [[Apple, 1], [airplane, 2]] list.sort(key=operator.itemgetter(0)) But, unfortunately, this will be case sensitive (Apple will come before airplane because the A is capital) and I need it to be insensitive. Try: list.sort(key=lambda el: el[0].lower()) BUT - it's not a good idea to use list as a name, 'cos list is a built-in, and you're obscuring it. -- Cheers, Simon B. [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues -- http://mail.python.org/mailman/listinfo/python-list
RE: Case-Insensitive Sorting of Multi-Dimensional Lists
Try: list.sort(key=lambda el: el[0].lower()) Thanks! Worked like a charm :) BUT - it's not a good idea to use list as a name, 'cos list is a built-in, and you're obscuring it. Oh, don't worry. That was strictly my portrayal of the problem. Thanks again! Jough -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
On 8 Jun, 14:18, Simon Brunning [EMAIL PROTECTED] wrote: On 6/7/07, Joe [EMAIL PROTECTED] wrote: I have a list of lists that I would like to sort utilizing a certain index of the nested list. I am able to successfully use: Import operator list = [[Apple, 1], [airplane, 2]] list.sort(key=operator.itemgetter(0)) But, unfortunately, this will be case sensitive (Apple will come before airplane because the A is capital) and I need it to be insensitive. Try: list.sort(key=lambda el: el[0].lower()) BUT - it's not a good idea to use list as a name, 'cos list is a built-in, and you're obscuring it. -- Cheers, Simon B. [EMAIL PROTECTED]://www.brunningonline.net/simon/blog/ GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues I have tried the following, for a one dimensional list and it works, but I can not get my head around this lambda. How would this be written, without the lamda ? mylist = ['Fred','bill','PAUL','albert'] mylist.sort(key=lambda el: el.lower()) Any pointer to an idiot's guide appreciated. Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
In [EMAIL PROTECTED], mosscliffe wrote: I have tried the following, for a one dimensional list and it works, but I can not get my head around this lambda. How would this be written, without the lamda ? Well ``lambda``\s are just anonymous functions so you can write it with a named function of course. mylist = ['Fred','bill','PAUL','albert'] mylist.sort(key=lambda el: el.lower()) So this becomes: def keyfunc(el): return el.lower() mylist.sort(key=keyfunc) Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
RE: Case-Insensitive Sorting of Multi-Dimensional Lists
Try: list.sort(key=lambda el: el[0].lower()) Now, I would like to be able to specify which index to sort by. I am not able to pass in external variables like: List.sort(key=lambda el: el[indexNumber].lower()) I am new to lambda and have searched for a few hours this morning, coming up empty handed. Is this possible? Thanks! Jough -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
On 6/8/07, Joe [EMAIL PROTECTED] wrote: Now, I would like to be able to specify which index to sort by. I am not able to pass in external variables like: List.sort(key=lambda el: el[indexNumber].lower()) Why ever not? -- Cheers, Simon B. [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues -- http://mail.python.org/mailman/listinfo/python-list
RE: Case-Insensitive Sorting of Multi-Dimensional Lists
Now, I would like to be able to specify which index to sort by. I am not able to pass in external variables like: List.sort(key=lambda el: el[indexNumber].lower()) Why ever not? Sorry, I should have written back with my findings. I had run into the problem described in this email: http://mail.python.org/pipermail/python-list/2001-June/090863.html. See, I was using bare execs elsewhere in the program. It had nothing to do with the lambda, which ended up working perfectly once I fixed the exec. Thanks! Jough -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
On 8 Jun, 16:39, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: In [EMAIL PROTECTED], mosscliffe wrote: I have tried the following, for a one dimensional list and it works, but I can not get my head around this lambda. How would this be written, without the lamda ? Well ``lambda``\s are just anonymous functions so you can write it with a named function of course. mylist = ['Fred','bill','PAUL','albert'] mylist.sort(key=lambda el: el.lower()) So this becomes: def keyfunc(el): return el.lower() mylist.sort(key=keyfunc) Ciao, Marc 'BlackJack' Rintsch Thanks, The mist is beginning to clear. Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: Case-Insensitive Sorting of Multi-Dimensional Lists
mosscliffe [EMAIL PROTECTED] writes: I have tried the following, for a one dimensional list and it works, but I can not get my head around this lambda. How would this be written, without the lamda ? mylist = ['Fred','bill','PAUL','albert'] mylist.sort(key=lambda el: el.lower()) Here's a hint: FoO.lower() 'foo' str.lower(FoO) 'foo' -- \When I was a baby I kept a diary. Recently I was re-reading | `\ it, it said 'Day 1: Still tired from the move. Day 2: Everybody | _o__) talks to me like I'm an idiot.' -- Steven Wright | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list