Python 如何遍历字母表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17182656/
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-19 00:41:50  来源:igfitidea点击:

How do I iterate through the alphabet?

pythonpython-2.7alphabet

提问by MillsOnWheels

In Python, could I simply ++ a char? What is an efficient way of doing this?

在 Python 中,我可以简单地 ++ 一个字符吗?这样做的有效方法是什么?

I want to iterate through URLs that have the www.website.com/term/#, www.website.com/term/a, www.website.com/term/b, www.website.com/term/c, www.website.com/term/d ... www.website.com/term/z format.

我想遍历具有 www.website.com/term/#、www.website.com/term/a、www.website.com/term/b、www.website.com/term/c、www .website.com/term/d ... www.website.com/term/z 格式。

采纳答案by Jared

You can use string.ascii_lowercasewhich is simply a convenience string of lowercase letters,

您可以使用string.ascii_lowercasewhich 只是一个方便的小写字母字符串,

>>> from string import ascii_lowercase
>>> for c in ascii_lowercase:
...     # append to your url

回答by Brian

In addition to string.ascii_lowercaseyou should also take a look at the ordand chrbuilt-ins. ord('a')will give you the ascii value for 'a'and chr(ord('a'))will give you back the string 'a'.

除了string.ascii_lowercase你还应该看看ordchr内置插件。ord('a')将为您提供 ascii 值'a'并将chr(ord('a'))返回字符串'a'.

Using these you can increment and decrement through character codes and convert back and forth easily enough. ASCII tableis always a good bookmark to have too.

使用这些,您可以通过字符代码递增和递减,并轻松地来回转换。ASCII 表也总是一个很好的书签。