![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
I want to sort a list of words case insensitive. Is there a good way?
I have: print "A list of Python builtin methods:" builtinList = dir(__builtins__) print builtinList
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
The sort method in lists allows you to specify a comparison function:
buildinList.sort(lambda a, b : cmp(a.lower(), b.lower())) def icmp(a, b):
return cmp(a.lower(), b.lower())
buildinList.sort(icmp) |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Thanks Arevos,
I was also looking at the new "key=" specifier. Is there something you can enter there to do the trick?
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
builtinList.sort(key = str.lower) |
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Strange, but it does work! You are a clever person after all!
__________________
I looked it up on the Intergnats! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|