在任一方向上循环一段时间

Ari*_*ide 4 python loops intervals

假设你想循环遍历两个边界ab(包括)之间的所有整数,但事先不知道如何a比较b.预期行为:

def run(a, b):
    if a < b:
        for i in range(a, b + 1):
            print i,
    elif a > b:
        for i in range(a, b - 1, -1):
            print i,
    else:
        print a
    print

run(3, 6)
run(6, 3)
run(5, 5)
Run Code Online (Sandbox Code Playgroud)

结果:

3 4 5 6
6 5 4 3
5
Run Code Online (Sandbox Code Playgroud)

有更优雅的解决方案吗?以下内容更简洁,但在以下情况下失败a == b:

def run(a, b):
    for i in range(a, b + cmp(b, a), cmp(b, a)):
        print i,
    print

run(3, 6)
run(6, 3)
run(5, 5)
Run Code Online (Sandbox Code Playgroud)

结果:

3 4 5 6
6 5 4 3
(...)
ValueError: range() step argument must not be zero
Run Code Online (Sandbox Code Playgroud)

jon*_*rpe 7

这适用于所有情况:

def run(a, b):
    """Iterate from a to b (inclusive)."""
    step = -1 if b < a else 1
    for x in xrange(a, b + step, step):
        yield x
Run Code Online (Sandbox Code Playgroud)

导致我采用这种表述的见解是,在你的两个案例中step调整b都是一样的; 一旦你有一个包容性的结束,你不需要特殊情况a == b.请注意,我已将其编写为生成器,因此它不仅仅是print结果,当您需要将其与其他代码集成时,它会更有用:

>>> list(run(3, 6))
[3, 4, 5, 6]
>>> list(run(6, 3))
[6, 5, 4, 3]
>>> list(run(5, 5))
[5]
Run Code Online (Sandbox Code Playgroud)

在Python 3.3+中使用生成器委派(参见PEP-380),这变得更加整洁:

def run(a, b):
    """Iterate from a to b (inclusive)."""
    step = -1 if b < a else 1
    yield from range(a, b + step, step)
Run Code Online (Sandbox Code Playgroud)

  • @Aristide`2*int(a <b) - 1`与`-1,如果b <a else 1'的长度完全相同,我认为后者更具可读性. (2认同)