On 05-04-2010 19:23, Robert Kern wrote: > On 2010-04-05 12:17 PM, Stef Mientki wrote: >> hello, >> >> AFAIK there's no case insensitive list in Python. >> By case insentive I mean that that sort and memebr of is case >> insensitive. >> >> Does soeone has a implementation of sucha case insensitive list ? > > mylist.sort(key=lambda x: x.lower()) > any(x.lower() == lowercase_query for x in mylist) >
thanks Robert, your code works perfectly, but I was thinking of something more easy to repeat and mor eidentical to existing lists. Or in other words, I want to use exactly the same commands and operators as with normal lists, like this _List = NoCase_List ( 'coala', 'donky' ) My_List.append ( 'Aap' ) My_List.append ( 'aapje' ) My_List.append ( 'beerthe' ) My_List.append ( 'BEER' ) print My_List My_List.sort () print My_List My_List.append ( 'aapJ' ) print My_List print My_List.index ( 'beer' ) print 'beer' in My_List print 'beert' in My_List After some trial and error the code below, gives almost the desired results. The only problem I see for now, is that replacing the items-list in wxpython widgets don't work, so I'l ask my question to the wxPython list. cheers, Stef class NoCase_List ( list ) : """ Case Insensitive List : The methods "sort" and "index" are case insensitive. The "in" operator works also case insensitive. After the list is sorted once, appending a new item keeps the list sorted. """ def __init__ ( self, *args, **kwargs ): self.Sorted = False list.__init__ ( self ) if isinstance ( args[0], list ) : for item in args [0] : self.append ( item ) else : self.append ( args[0] ) def append ( self, value ) : list.append ( self, value ) if self.Sorted : self.sort () def sort ( self, *args, **kwargs ) : self.Sorted = True def _sort ( a, b ) : return cmp ( a.lower(), b.lower() ) return list.sort ( self, _sort ) def index ( self, value ) : value = value.lower() for i, item in enumerate ( self ) : if item.lower() == value : return i else : return -1 def __contains__ ( self, value ) : return self.index ( value ) >= 0
-- http://mail.python.org/mailman/listinfo/python-list