HI guys,
How do you write Perl's
print a ... z, A ... Z, \n' in Python
In Python?
A way I came up with is the following, but I'm sure this is ugly.
''.join(chr(c) for c in (range(ord('a'), ord('z')+1) +
range(ord('A'), ord('Z')+1)))
or
crange = lambda c1, c2: [ chr(c) for c in
How do you write Perl's
print a ... z, A ... Z, \n' in Python
In Python?
you might consider this cheating, but it's packed with zen goodness:
import string
print string.letters
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
--
js:
crange = lambda c1, c2: [ chr(c) for c in range(ord(c1), ord(c2)+1) ]
''.join(chr(c) for c in crange('a', 'z') + crange('A', 'Z'))
Yes, managing char ranges is a bit of pain with Python.
You can also pack those chars:
xcrange = lambda cc: (chr(c) for c in xrange(ord(cc[0]), ord(cc[1])
+1))
But note that you return the last item of the range too, and that goes
against the semantic of the usual Python range/xrange, so you may want
to call this function with another name.
That makes sense. 100% agree with you.
Maybe there are better ways to solve this problem. Maybe a way to
I forgot to cc pythonlist...
#
Thanks for you quick reply.
I didn't know any string constants.
From Python Library reference, 4.1.1 String constants:
letters
The concatenation of the strings lowercase and uppercase described below.
The specific value is
js wrote:
A way I came up with is the following, but I'm sure this is ugly.
You could abuse __getitem__ (terribly, heh!) and use slice syntax...
class crange():
def __init__(self):
self.valid = range(47,58) + range(65,91) + range(97,123)
def __getitem__(self, s):
if
js [EMAIL PROTECTED] writes:
But note that you return the last item of the range too, and that
goes against the semantic of the usual Python range/xrange, so you
may want to call this function with another name.
That makes sense. 100% agree with you.
Maybe there are better ways to solve
Maybe we don't want char range If string constants would be rich
enough.
But as soon as we want a string that doesn't correspond to any
pre-defined constants, we're hosed. For example, there isn't
a constant that would correspond to this Perl-ism:
print l ... w, e ... j, L ... W, E
js [EMAIL PROTECTED] wrote:
HI guys,
How do you write Perl's
print a ... z, A ... Z, \n' in Python
In Python?
This specific one is easy, though this doesn't generalize:
import string
print string.lowercase + string.uppercase
For the general case, there's no way to avoid calling
js [EMAIL PROTECTED] wrote:
I forgot to cc pythonlist...
#
Thanks for you quick reply.
I didn't know any string constants.
From Python Library reference, 4.1.1 String constants:
letters
The concatenation of the strings lowercase and uppercase described
[EMAIL PROTECTED] (Alex Martelli) wrote in
news:[EMAIL PROTECTED]:
js [EMAIL PROTECTED] wrote:
HI guys,
How do you write Perl's
print a ... z, A ... Z, \n' in Python
In Python?
This specific one is easy, though this doesn't generalize:
import string
print
rzed [EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] (Alex Martelli) wrote in
news:[EMAIL PROTECTED]:
js [EMAIL PROTECTED] wrote:
HI guys,
How do you write Perl's
print a ... z, A ... Z, \n' in Python
In Python?
This specific one is easy, though this doesn't
12 matches
Mail list logo