两个未分类数字之间的范围

Ant*_*nto 1 python range

我需要找到两个变量之间的范围,

def find_range(a,b):
    #some process
    for i in range(a,b):
        #some process
Run Code Online (Sandbox Code Playgroud)

但问题是数字可能是a=4,b=2,在这种情况下我需要2到4之间的范围.
我知道我可以通过使用if语句解决这个并找到最小数字但我不想使用if语句,因为已经有很多的if陈述.
这可能吗?

Ash*_*ary 5

你可以使用sorted和tuple解包:

def find_range(a,b):
    print range(*sorted((a,b)))
Run Code Online (Sandbox Code Playgroud)

演示:

>>> find_range(4, 2)
[2, 3]
>>> find_range(2, 4)
[2, 3]
Run Code Online (Sandbox Code Playgroud)