( 2.31.New operators: 'eq', 'ne', 'last', '..' ) ?
seanb at home.com
seanb at home.com
Fri Jan 21 12:56:03 EST 2000
On 21 Jan, Gerrit Holl wrote:
> Stefan Schwarzer wrote on 948334133:
>> Perhaps the ".." operator is something to consider (if it isn't
>> incompatible with somethings else in the language), but most of
>> the times "range" does it's job very well.
>
> Unfortunately, not yet. It's quite hard to get a string which
> would be a..k in Perl. Maybe a nice feature for range()?
>>>> string.join(map(lambda i: chr(i), range(ord('a'), ord('k'))), '')
> 'abcdefghij'
>
> Doesn't look very well, does it?
>
> regards,
> Gerrit.
>
As an aside, you do know that that lambda is completely unncecessarry,
right? The following code is equivalent and (a little) simpler:
>>import string
>>string.join(map(chr, range(ord('a'), ord('k'))), '')
Personally, I would prefer to abstract this into a reusable function:
>>>def srange(start_char, end_char, step=1)
... '''Create a string range from two given characters.'''
... start, end = map(ord, (start_char, end_char))
... chars = map(chr, range(start, end, step))
... return string.join(chars, '')
--
Sean Blakey
(206)297-7123
quine = ['print "quine =",quine,"; exec(quine[0])"']; exec(quine[0])
More information about the Python-list
mailing list