开发者

What is the python equivalent to perl "a".."azc"

In perl, to get a list of all strings from "a" to "azc", to only thing to do is using the range operator:

perl -le 'print "a".."azc"'

What I want is a list of strings:

["a", "b", ..., "z", "aa", ..., "az" ,"ba", ..., "azc"]

I suppose I can use ord and chr, looping over and over, this is simple to get for "a" to "z", eg:

>>> [chr(c) for c in range(ord("a"), ord("z") + 1)]
['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']

But a bit more complex for my case, 开发者_运维知识库here.

Thanks for any help !


Generator version:

from string import ascii_lowercase
from itertools import product

def letterrange(last):
    for k in range(len(last)):
        for x in product(ascii_lowercase, repeat=k+1):
            result = ''.join(x)
            yield result
            if result == last:
                return

EDIT: @ihightower asks in the comments:

I have no idea what I should do if I want to print from 'b' to 'azc'.

So you want to start with something other than 'a'. Just discard anything before the start value:

def letterrange(first, last):
    for k in range(len(last)):
        for x in product(ascii_lowercase, repeat=k+1):
            result = ''.join(x)
            if first:
                if first != result:
                    continue
                else:
                    first = None
            yield result
            if result == last:
                return


A suggestion purely based on iterators:

import string
import itertools

def string_range(letters=string.ascii_lowercase, start="a", end="z"):
    return itertools.takewhile(end.__ne__, itertools.dropwhile(start.__ne__, (x for i in itertools.count(1) for x in itertools.imap("".join, itertools.product(letters, repeat=i)))))

print list(string_range(end="azc"))


Use the product call in itertools, and ascii_letters from string.

from string import ascii_letters
from itertools import product

if __name__ == '__main__':
    values = []
    for i in xrange(1, 4):
        values += [''.join(x) for x in product(ascii_letters[:26], repeat=i)]

    print values


Here's a better way to do it, though you need a conversion function:

for i in xrange(int('a', 36), int('azd', 36)):
    if base36encode(i).isalpha():
        print base36encode(i, lower=True)

And here's your function (thank you Wikipedia):

def base36encode(number, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', lower=False):
    '''
    Convert positive integer to a base36 string.
    '''
    if lower:
        alphabet = alphabet.lower()
    if not isinstance(number, (int, long)):
        raise TypeError('number must be an integer')
    if number < 0:
        raise ValueError('number must be positive')

    # Special case for small numbers
    if number < 36:
        return alphabet[number]

    base36 = ''
    while number != 0:
        number, i = divmod(number, 36)
        base36 = alphabet[i] + base36

    return base36

I tacked on the lowercase conversion option, just in case you wanted that.


I generalized the accepted answer to be able to start middle and to use other than lowercase:

from string import ascii_lowercase, ascii_uppercase
from itertools import product

def letter_range(first, last, letters=ascii_lowercase):
    for k in range(len(first), len(last)):
        for x in product(letters, repeat=k+1):
            result = ''.join(x)
            if len(x) != len(first) or result >= first:
                yield result
                if result == last:
                    return
print list(letter_range('a', 'zzz'))
print list(letter_range('BA', 'DZA', ascii_uppercase))


def strrange(end):
    values = []
    for i in range(1, len(end) + 1):
        values += [''.join(x) for x in product(ascii_lowercase, repeat=i)]
    return values[:values.index(end) + 1]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜