def main():
f=['orange','zoo','apple','internationalization','blueberry'] #python 内置的排序方式默认为升序(从小到大)
#如果想要降序 用reverse参数来制定
#python中的函数几乎没有副作用的函数
#调用函数之后不会影响传入的参数
f2 = sorted (f,reverse=True)
print(f)
print(f2)
f.reverse()
f3=(reversed(f))
print(f3)
f4=sorted(f,key=str_len,reverse=True)
print(f4)
结果:
['orange', 'zoo', 'apple', 'internationalization', 'blueberry']
['zoo', 'orange', 'internationalization', 'blueberry', 'apple']
<list_reverseiterator object at 0x000000000259E9B0>
['internationalization', 'blueberry', 'orange', 'apple', 'zoo']
如:fibonacci函数
def fib1(n): d=[1,1] for num in range (2,n): val=d[num-1]+d[num-2] d.append(val) print(d) print(d[::2]) print(d[-1:-3:-1]) d.reverse() print(d) if __name__ == '__main__': fib1(20) 结果: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765] [1, 2, 5, 13, 34, 89, 233, 610, 1597, 4181] [6765, 4181] [6765, 4181, 2584, 1597, 987, 610, 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1, 1]