Python中的字母范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16060899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:41:47  来源:igfitidea点击:

Alphabet range in Python

pythonstringlistalphabet

提问by Alexa Elis

Instead of making a list of alphabet characters like this:

而不是像这样制作字母字符列表:

alpha = ['a', 'b', 'c', 'd'.........'z']

is there any way that we can group it to a range or something? For example, for numbers it can be grouped using range():

有什么方法可以将它分组到一个范围或其他东西?例如,对于数字,可以使用range()以下方法对其进行分组:

range(1, 10)

采纳答案by jamylak

>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

If you really need a list:

如果你真的需要一个列表:

>>> list(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']

And to do it with range

并用它来做 range

>>> list(map(chr, range(97, 123))) #or list(map(chr, 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']

Other helpful stringmodule features:

其他有用的string模块功能:

>>> help(string) # on Python 3
....
DATA
    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    octdigits = '01234567'
    printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~ \t\n\r\x0b\x0c'
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\]^_`{|}~'
    whitespace = ' \t\n\r\x0b\x0c'

回答by Trinh Nguyen

In Python 2.7 and 3 you can use this:

在 Python 2.7 和 3 中,你可以使用这个:

import string
string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'

string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

As @Zaz says: string.lowercaseis deprecated and no longer works in Python 3 but string.ascii_lowercaseworks in both

正如@Zaz 所说: string.lowercase已弃用,不再适用于 Python 3,但string.ascii_lowercase适用于两者

回答by Bg1850

[chr(i) for i in range(ord('a'),ord('z')+1)]

回答by pylang

Here is a simple letter-range implementation:

这是一个简单的字母范围实现:

Code

代码

def letter_range(start, stop="{", step=1):
    """Yield a range of lowercase letters.""" 
    for ord_ in range(ord(start.lower()), ord(stop.lower()), step):
        yield chr(ord_)

Demo

演示

list(letter_range("a", "f"))
# ['a', 'b', 'c', 'd', 'e']

list(letter_range("a", "f", step=2))
# ['a', 'c', 'e']

回答by Qaswed

If you are looking to an equivalent of letters[1:10]from R, you can use:

如果您正在寻找等效letters[1:10]于 R 的内容,您可以使用:

 import string
 list(string.ascii_lowercase[0:10])

回答by lakshmikandan

Print the Upper and Lower case alphabets in python using a built-in range function

使用内置范围函数在python中打印大写和小写字母

def upperCaseAlphabets():
    print("Upper Case Alphabets")
    for i in range(65, 91):
        print(chr(i), end=" ")
    print()

def lowerCaseAlphabets():
    print("Lower Case Alphabets")
    for i in range(97, 123):
        print(chr(i), end=" ")

upperCaseAlphabets();
lowerCaseAlphabets();

回答by RicarHincapie

This is the easiest way I can figure out:

这是我能弄清楚的最简单的方法:

#!/usr/bin/python3 for i in range(97, 123): print("{:c}".format(i), end='')

#!/usr/bin/python3 for i in range(97, 123): print("{:c}".format(i), end='')

So, 97 to 122 are the ASCII number equivalent to 'a' to and 'z'. Notice the lowercase and the need to put 123, since it will not be included).

因此,97 到 122 是等效于 'a' to 和 'z' 的 ASCII 数字。注意小写和需要输入 123,因为它不会被包括在内)。

In print function make sure to set the {:c}(character) format, and, in this case, we want it to print it all together not even letting a new line at the end, so end=''would do the job.

在打印功能中确保设置{:c}(字符)格式,在这种情况下,我们希望它一起打印,甚至不让末尾换行,这样end=''就可以了。

The result is this: abcdefghijklmnopqrstuvwxyz

结果是这样的: abcdefghijklmnopqrstuvwxyz