范围超过字符在python [英] range over character in python

查看:255
本文介绍了范围超过字符在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有方法来覆盖字符?

Is there an way to range over characters? something like this.

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

我希望你们能帮助。

推荐答案

这是一个很好用的自定义生成器:

This is a great use for a custom generator:

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)

then:

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

这篇关于范围超过字符在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆