Is it possible to set a range of letters in python?

Is there a way to make a range of letters in python as follows:

for x in range(a,h,)
+5
source share
8 answers

Sort of:

[chr(i) for i in range(ord('a'),ord('h'))]

Will list alphabetical characters for iteration, which you can then use in a loop

for x in [chr(i) for i in range(ord('a'),ord('h'))]:
    print(x)

or it will do the same:

for x in map(chr, range(*map(ord,['a', 'h']))):
    print(x)
+14
source

You can use ord()to convert letters to character ordinals and vice versa:

def char_range(start, end, step=1):
    for char in range(ord(start), ord(end), step):
        yield chr(char)

Everything seems to work just fine:

>>> ''.join(char_range('a', 'z'))
    'abcdefghijklmnopqrstuvwxy'
+4
source

, :

def letter_range(start, stop):
    for c in xrange(ord(start), ord(stop)):
        yield chr(c)


for x in letter_range('a', 'h'):
    print x,

:

a b c d e f g
+2
import string

def letter_range(f,l,al = string.ascii_lowercase):
    for x in al[al.index(f):al.index(l)]:
        yield x

print ' '.join(letter_range('a','h'))

a b c d e f g
+2

/ ( , ):

letters = 'abcdefghijklmnopqrstuvwxyz'
for each in letters:
    print each

result:
a
b
c
...
z
+1

Emanuele , , , , . : aa... zz. , , -, , , , say 'y' 'af' ( 'z' 'aa'). , , , .

def strange(start, end_or_len, sequence='ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
    """Create a generator of a range of 'sequential' strings from
    start to end_or_len if end_or_len is a string or containing
    end_or_len entries if end_or_len is an integer.

    >>> list(strange('D', 'F'))
    ['D', 'E', 'F']
    >>> list(strange('Y', 'AB'))
    ['Y', 'Z', 'AA', 'AB']
    >>> list(strange('Y', 4))
    ['Y', 'Z', 'AA', 'AB']
    >>> list(strange('A', 'BAA', sequence='AB'))
    ['A', 'B', 'AA', 'AB', 'BA', 'BB', 'AAA', 'AAB', 'ABA', 'ABB', 'BAA']
    >>> list(strange('A', 11, sequence='AB'))
    ['A', 'B', 'AA', 'AB', 'BA', 'BB', 'AAA', 'AAB', 'ABA', 'ABB', 'BAA']
    """
    seq_len = len(sequence)
    start_int_list = [sequence.find(c) for c in start]
    if isinstance(end_or_len, int):
        inclusive = True
        end_int_list = list(start_int_list)
        i = len(end_int_list) - 1
        end_int_list[i] += end_or_len - 1
        while end_int_list[i] >= seq_len:
            j = end_int_list[i] // seq_len
            end_int_list[i] = end_int_list[i] % seq_len
            if i == 0:
                end_int_list.insert(0, j-1)
            else:
                i -= 1
                end_int_list[i] += j
    else:
        end_int_list = [sequence.find(c) for c in end_or_len]
    while len(start_int_list) < len(end_int_list) or start_int_list <= end_int_list:
        yield ''.join([sequence[i] for i in start_int_list])
        i = len(start_int_list)-1
        start_int_list[i] += 1
        while start_int_list[i] >= seq_len:
            start_int_list[i] = 0
            if i == 0:
                start_int_list.insert(0,0)
            else:
                i -= 1
                start_int_list[i] += 1


if __name__ =='__main__':
    import doctest
    doctest.testmod()
+1

?

import string

s = string.ascii_lowercase
print( s[ s.index('b'):s.index('o')+1 ] )
0

Malcom , - , Pythons. "A" "Z" "ZZ" "ZZZ", .

"AA" <"Z" "AAA" <"ZZ" .

Python [0,0,0] , [1,1] "<" ">".

while len(start_int_list) < len(end_int_list) or start_int_list <= end_int_list:

,

while len(start_int_list) < len(end_int_list) or\
        ( len(start_int_list) == len(end_int_list) and start_int_list <= end_int_list):

https://docs.python.org/3/tutorial/datastructures.html#comparing-sequence-and-other-types

.

def strange(start, end_or_len, sequence='ABCDEFGHIJKLMNOPQRSTUVWXYZ'):

    seq_len = len(sequence)
    start_int_list = [sequence.find(c) for c in start]
    if isinstance(end_or_len, int):
        inclusive = True
        end_int_list = list(start_int_list)
        i = len(end_int_list) - 1
        end_int_list[i] += end_or_len - 1
        while end_int_list[i] >= seq_len:
            j = end_int_list[i] // seq_len
            end_int_list[i] = end_int_list[i] % seq_len
            if i == 0:
                end_int_list.insert(0, j-1)
            else:
                i -= 1
                end_int_list[i] += j
    else:
        end_int_list = [sequence.find(c) for c in end_or_len]

    while len(start_int_list) < len(end_int_list) or\
         (len(start_int_list) == len(end_int_list) and start_int_list <= end_int_list):**
        yield ''.join([sequence[i] for i in start_int_list])
        i = len(start_int_list)-1
        start_int_list[i] += 1
        while start_int_list[i] >= seq_len:
            start_int_list[i] = 0
            if i == 0:
                start_int_list.insert(0,0)
            else:
               i -= 1
                start_int_list[i] += 1

, Malcom - , Python.

0

All Articles