Jeremy
Jeremy

Reputation: 393

What's the pythonic way of generating a range of chars?

In other languages, I would use a construct like this:

a..z

I couldn't come up with a better solution than this:

[chr(x) for x in range(ord("a"), ord("z") + 1)]

Is there a shorter, more readable way of building such a list ?

Upvotes: 9

Views: 242

Answers (7)

Mayur
Mayur

Reputation: 3097

import random

def random_chars(size=10, chrs=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chrs) for x in range(size))

Upvotes: 0

Óscar López
Óscar López

Reputation: 236004

Yet another option:

from string import ascii_lowercase

def charRange(start, end):
    offset = ord('a')
    return ascii_lowercase[ord(start)-offset:ord(end)-offset+1]

charRange('a', 'f')
> 'abcdef'

Upvotes: 1

GreenMatt
GreenMatt

Reputation: 18580

As noted elsewhere, the letters 'a' through 'z' are already in string.lowercase. Thus you can just take a slice of that.

import string
def create_letter_list(start_char, end_char):
    return string.lowercase[ord(start_char) - ord('a'): ord(end_char) - ord('a') + 1]

letter_list = create_letter_list('g', 'n')
 print letter_list

Output:

ghijklmn

Upvotes: 0

Jan
Jan

Reputation: 735

There's nothing wrong with your list-comprehension, but maybe you want to try:

from string import lowercase
print lowercase
  abcdefghijklmnopqrstuvwxyz
print list(lowercase)
  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

As noted by julio.alegria and Nathan Binkert lowercase is a string of a all lower-case letters and Python strings are iterables.

If you require subsets of the alphabet, there are nice and Pythonic ways to do this:

print lowercase.find('f')
  5
print lowercase[5,9]
  fghij
print filter(lambda c: ord('f') <= ord(c) <= ord('j'), lowercase)
  fghij

Upvotes: 1

RanRag
RanRag

Reputation: 49557

My way is similar to yours but you can use map and create an arbitrary function like this

>>> def generate_list(char1,char2):
...      myl = map(chr, range(ord(char1),ord(char2)+1))
...      print myl
...
>>> generate_list("a","d")
['a', 'b', 'c', 'd']

Upvotes: 1

Yann Vernier
Yann Vernier

Reputation: 15877

You can replace the list comprehension with a map(chr,range(ord('a'),ord('z')+1)) call, but that's perhaps not what you're really asking. The example you show, however, is more appropriately imported; it is available as a string in string.lowercase.

Assuming letters match up with ordinals may be a mistake, considering letters like ß and æ, which may be sorted differently depending on locales (i.e. åäö are last in the alphabet for Swedish, but ä and ö are variants of a and o in German). That's why we have functions like unicode.islower() and locale.strcoll().

Upvotes: 0

Nathan Binkert
Nathan Binkert

Reputation: 9124

Not necessarily great if you want to do something other than a to z, but you can do this:

from string import ascii_lowercase
for c in ascii_lowercase:
    print c

Upvotes: 6

Related Questions