generating a liste of characters

S

Shane Geiger

import string
alphabet=list(string.letters[0:26])
print alphabet



Yves said:
Is there any built in way to generate a list of characters, something
along the line of range('a'-'z') ?

Right now I am using:

chars = [ chr(l) for l in range(0x30, 0x3a) ] # 0 - 9
chars += [ chr(l) for l in range(0x41, 0x5b) ] # A - Z
chars += [ chr(l) for l in range(0x61, 0x7b) ] # a - z

Is there a better, more straight forward way of doing that ?



Thanks.



Yves.
http://www.sollers.ca/blog/2008/swappiness
http://www.sollers.ca/blog/2008/swappiness/.fr


--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
J

James Mills

Is there any built in way to generate a list of characters, something
along the line of range('a'-'z') ?

Right now I am using:

chars = [ chr(l) for l in range(0x30, 0x3a) ] # 0 - 9
chars += [ chr(l) for l in range(0x41, 0x5b) ] # A - Z
chars += [ chr(l) for l in range(0x61, 0x7b) ] # a - z

Is there a better, more straight forward way of doing that ?

cheers
James
 
V

Vlastimil Brom

2008/12/3 Yves Dorfsman said:
Is there any built in way to generate a list of characters, something
along the line of range('a'-'z') ?

Right now I am using:

chars = [ chr(l) for l in range(0x30, 0x3a) ] # 0 - 9
chars += [ chr(l) for l in range(0x41, 0x5b) ] # A - Z
chars += [ chr(l) for l in range(0x61, 0x7b) ] # a - z

Is there a better, more straight forward way of doing that ?



Thanks.



Yves.
http://www.sollers.ca/blog/2008/swappiness
http://www.sollers.ca/blog/2008/swappiness/.fr

If you want tu use it generaly an any characters, maybe ord() can make
it a bit simpler:[u'a', u'b', u'c', u'd', u'e', u'f', u'g', u'h', u'i', u'j', u'k',
u'l', u'm', u'n', u'o', u'p', u'q', u'r', u's', u't', u'u', u'v',
u'w', u'x', u'y']
otherewise see the string module with some predefined ranges.

hth,
vbr
 
M

Matt Nordhoff

This is a slightly old post, but oh well...

Shane said:
import string
alphabet=list(string.letters[0:26])
print alphabet

Most of the string module's constants are locale-specific. If you want
the ASCII alphabet instead of the current language's, you need to use
string.ascii_{letters,lowercase,uppercase}.
Yves said:
Is there any built in way to generate a list of characters, something
along the line of range('a'-'z') ?

Right now I am using:

chars = [ chr(l) for l in range(0x30, 0x3a) ] # 0 - 9
chars += [ chr(l) for l in range(0x41, 0x5b) ] # A - Z
chars += [ chr(l) for l in range(0x61, 0x7b) ] # a - z

Is there a better, more straight forward way of doing that ?



Thanks.
--
 
M

Mark Tolonen

Yves Dorfsman said:
Is there any built in way to generate a list of characters, something
along the line of range('a'-'z') ?

Right now I am using:

chars = [ chr(l) for l in range(0x30, 0x3a) ] # 0 - 9
chars += [ chr(l) for l in range(0x41, 0x5b) ] # A - Z
chars += [ chr(l) for l in range(0x61, 0x7b) ] # a - z

Is there a better, more straight forward way of doing that ?

Writing a helper function reduces code repetition and improves readability:

def crange(startch,endch):
'''Return a list of characters from startch to endch, inclusive.'''
return [chr(c) for c in xrange(ord(startch),ord(endch)+1)]

chars = crange('0','9') + crange('A','Z') + crange('a','z')

-Mark
 
B

bearophileHUGS

Mark Tolonen:
Writing a helper function reduces code repetition and improves readability:
    def crange(startch,endch):
        '''Return a list of characters from startch to endch, inclusive.'''
        return [chr(c) for c in xrange(ord(startch),ord(endch)+1)]

In Python ranges are open on the right, so I name cinterval such
function.

Bye,
bearophile
 
M

Mark Tolonen

Mark Tolonen:
Writing a helper function reduces code repetition and improves
readability:
def crange(startch,endch):
'''Return a list of characters from startch to endch, inclusive.'''
return [chr(c) for c in xrange(ord(startch),ord(endch)+1)]

In Python ranges are open on the right, so I name cinterval such
function.

Yes, and that's fine when dealing with integers and slicing, but when
dealing with characters, it is non-obvious what character to use. What
"looks" correct?

chars = crange('0','9') + crange('A','Z') + crange('a','z') # inclusive

or

chars = crange('0',':') + crange('A','[') + crange('a','{') # open right

or if you didn't know the character after the one you actually wanted:

chars = crange('0',chr(ord('9')+1)) + crange('A',chr(ord('Z')+1)) +
crange('a',chr(ord('z')+1)) # open right, but messy

-Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top