一种使用Python的方法,我不知道

时间:2012-11-11 02:35:53

标签: python

在此快速排序功能中:

def qsort2(list):
    if list == []: 
        return []
    else:
        pivot = list[0]
        # can't understand the following line
        lesser, equal, greater = partition(list[1:], [], [pivot], [])
    return qsort2(lesser) + equal + qsort2(greater)

def partition(list, l, e, g):
    if list == []:
        return (l, e, g)
    else:
        head = list[0]
        if head < e[0]:
            return partition(list[1:], l + [head], e, g)
        elif head > e[0]:
            return partition(list[1:], l, e, g + [head])
        else:
            return partition(list[1:], l, e + [head], g)

我不理解评论下方的句子。有人能告诉我这句话的意思吗?

1 个答案:

答案 0 :(得分:8)

它将一个元组解包为三个变量。

def foo():
    return (1, 2, 3)

a, b, c = foo()
print(a) # prints "1"
print(b) # prints "2"
print(c) # prints "3"