Python语言程序设计:Lab6

Reversing a List


If you have time, you can try to write a function which will reverse a list recursively, according to the
following algorithm:
1. reverse( [] ) is []
2. reverse( list ) is [ last_element ] + reverse( list without last element )

def reverse_a_list(list):
    if len(list)==0:
        return [];
    else:
        return(reverse_a_list(list[1:])+[list[0]])
def reverse_a_list2(list):
    if len(list)==0:
        return []
    else:
        return([list[-1]]+reverse_a_list2(list[0:-1]))

猜你喜欢

转载自www.cnblogs.com/zijeak/p/11654660.html
今日推荐