<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
On 05-04-2010 19:23, Robert Kern wrote:
<blockquote cite="mid:hpd69u$4kp$1@dough.gmane.org" type="cite">On
2010-04-05 12:17 PM, Stef Mientki wrote:
<br>
<blockquote type="cite">hello,
<br>
<br>
AFAIK there's no case insensitive list in Python.
<br>
By case insentive I mean that that sort and memebr of is case
insensitive.
<br>
<br>
Does soeone has a implementation of sucha case insensitive list ?
<br>
</blockquote>
<br>
mylist.sort(key=lambda x: x.lower())
<br>
any(x.lower() == lowercase_query for x in mylist)
<br>
<br>
</blockquote>
<br>
<font size="+1">thanks Robert,<br>
your code works perfectly,<br>
but I was thinking of something more easy to repeat and mor eidentical
to existing lists.<br>
Or in other words, I want to use exactly the same commands and
operators as with normal lists,<br>
like this<br>
</font><font size="+1"><tt> _List = NoCase_List ( 'coala', 'donky' )<br>
My_List.append ( 'Aap' )<br>
My_List.append ( 'aapje' )<br>
My_List.append ( 'beerthe' )<br>
My_List.append ( 'BEER' )<br>
print My_List<br>
My_List.sort ()<br>
print My_List<br>
My_List.append ( 'aapJ' )<br>
print My_List<br>
print My_List.index ( 'beer' )<br>
print 'beer' in My_List<br>
print 'beert' in My_List<br>
</tt></font><font size="+1"><br>
After some trial and error the code below, gives almost the desired
results.<br>
<br>
The only problem I see for now,<br>
is that replacing the items-list in wxpython widgets don't work,<br>
so I'l ask my question to the wxPython list.<br>
<br>
cheers,<br>
Stef<br>
<br>
<br>
<br>
</font><font size="+1"><tt>class NoCase_List ( list ) :<br>
"""<br>
Case Insensitive List :<br>
The methods "sort" and "index" are case insensitive.<br>
The "in" operator works also case insensitive.<br>
After the list is sorted once,<br>
appending a new item keeps the list sorted.<br>
"""<br>
def __init__ ( self, *args, **kwargs ):<br>
self.Sorted = False<br>
list.__init__ ( self )<br>
if isinstance ( args[0], list ) :<br>
for item in args [0] :<br>
self.append ( item )<br>
else :<br>
self.append ( args[0] )<br>
<br>
def append ( self, value ) :<br>
list.append ( self, value )<br>
if self.Sorted :<br>
self.sort ()<br>
<br>
def sort ( self, *args, **kwargs ) :<br>
self.Sorted = True<br>
def _sort ( a, b ) :<br>
return cmp ( a.lower(), b.lower() )<br>
return list.sort ( self, _sort )<br>
<br>
def index ( self, value ) :<br>
value = value.lower()<br>
for i, item in enumerate ( self ) :<br>
if item.lower() == value :<br>
return i<br>
else :<br>
return -1<br>
<br>
def __contains__ ( self, value ) :<br>
return self.index ( value ) >= 0<br>
</tt></font><font size="+1"><br>
</font>
</body>
</html>