Expand alphabetical range to list of characters in Python

მაგალითად დაბეჭდოს a დან f  მდე
method 1.

lowercase

>>>a = 'a-f'
>>>s = list(chr, map(range(ord(a[0]), ord(a[-1])+1)))
>>>print(s)
>>>['a', 'b', 'c', 'd', 'e', 'f']

uppercase

>>>a = 'A-F'
>>>s = list(chr, map(range(ord(a[0]), ord(a[-1])+1)))
>>>print(s)
>>>['A', 'B', 'C', 'D', 'E', 'F']

method 2.

>>>a = 'A-F'
>>>s = [chr(item) for item in range(ord(a[0]), ord(a[-1])+1)]

>>>print(s)
>>>['A', 'B', 'C', 'D', 'E', 'F']

method 3.


>>>import string
>>>def alphabet(strs):
        upper=string.ascii_uppercase
        lower=string.ascii_lowercase

         if strs[0] in upper:      
              return list(upper[upper.index(strs[0]): upper.index(strs[-1])+1])
        if strs[0] in lower:
             return list(lower[lower.index(strs[0]): lower.index(strs[-1])+1])

>>>print(alphabet('a-f'))
>>>print(alphabet('A-F'))
output
>>>

['a', 'b', 'c', 'd','e','f']
['A','B', 'C', 'D', 'E', 'F']



--------------------
>>> import string
>>> ','.join(string.ascii_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' 







SHARE

About Lasha Gogua

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment