python - python中的字符范围

标签 python range character

有没有办法跨越字符?像这样的。

for c in xrange( 'a', 'z' ):
    print c

希望大家帮忙

最佳答案

这是自定义生成器的一大用途:

Python 2:

def char_range(c1, c2):
    """Generates the characters from `c1` to `c2`, inclusive."""
    for c in xrange(ord(c1), ord(c2)+1):
        yield chr(c)

然后:

for c in char_range('a', 'z'):
    print c

Python 3:

def char_range(c1, c2):
    """Generates the characters from `c1` to `c2`, inclusive."""
    for c in range(ord(c1), ord(c2)+1):
        yield chr(c)

然后:

for c in char_range('a', 'z'):
    print(c)

关于python - python中的字符范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001144/

相关文章:

java - Java将随机字符组合成字符串

C简单数组访问比较

python - MAC | Python 脚本运行良好,在终端插入 Mysql 表但无法作为 Cron 作业运行 |苹果

linq - 分组为连续整数范围

Excel VBA 工作表.名称与工作表.范围

python - 类型错误: 'range' 对象在没有明显原因的情况下不可调用

c++ - 为什么字符 'a' 没有自动转换为 97?

python - Sphinx HTML 输出的搜索功能的不同 URL 参数有什么影响?

python - 寻找最接近值算法

python - 将 4 个 numpy 数组中的坐标写入文件 Python