caseless dictionary howto ?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 19 22:17:07 EDT 2007


En Tue, 19 Jun 2007 19:40:10 -0300, Steven Bethard  
<steven.bethard at gmail.com> escribió:

> Stef Mientki wrote:
>> Evan Klitzke wrote:
>>> On 6/19/07, Stef Mientki <S.Mientki-nospam at mailbox.kun.nl> wrote:
>>>>
>>>> I need to search a piece of text and make all words that are equal
>>>> (except their case) also equal in their case, based on the first
>>>> occurrence.

> ...     def __setitem__(self, key, value):
> ...         self._dict[key.lower()] = value
> ...         if key not in self._original_keys:
> ...             self._original_keys[key.lower()] = key
> ...
>
> Note that because I store the first form encountered for every key, I
> can always use the lower-case version to retrieve the "original string".

As written, it stores the last used spelling; a small change is required  
to get the intended behavior:

> ...     def __setitem__(self, key, value):
> ...         self._dict[key.lower()] = value
> ...         if key.lower() not in self._original_keys:
> ...             self._original_keys[key.lower()] = key

-- 
Gabriel Genellina




More information about the Python-list mailing list