Why Python does *SLICING* the way it does??
Roy Smith
roy at panix.com
Fri Apr 22 13:22:09 EDT 2005
In article <3csphpF6es7iuU1 at individual.net>,
Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
>Roy Smith wrote:
>
>>>Other possibility, probably faster when almost all keys in the range are in
>>>the dictionary:
>>>
>>>class sdict(dict):
>>> def __getitem__(self, index):
>>> if isinstance(index, slice):
>>> d = {}
>>> for key in xrange(slice.start, slice.stop, slice.step):
>>> if key in self:
>>> d[key] = self[key]
>>> return d
>>> else:
>>> return dict.__getitem__(self, index)
>>
>> The problem with that is it requires the keys to be integers.
>
>Yes, but wasn't it thought as a replacement for a list?
Originally, yes. I went off on a tangent with string-keyed slices.
More information about the Python-list
mailing list