def fac(n):
'''return n! zw'''
return 1 if n <2 else n*fac(n-1)
print(fac.__doc__)
fa=fac
a=list(map(fa,range(5)))
print('this is origin a:',a)
b=sorted(a,reverse=True)
print('this is a using sorted ',b)
print('this is a by using sorted :',a)
c=list.sort(a,reverse=True)
print('this is c',c)
print('this is a by using list sort',a)
fruit =['b','dbc','eb','abcdef','gbcd']
sorted_fruit =sorted(fruit)
print('默认字符按照字母顺序排序:',sorted_fruit)
sorted_len_fruit =sorted(fruit,key=len)
print('按照字符的长度排序',sorted_len_fruit)
compound = list(map(fa,filter(lambda n:n%3,range(6))))
print('多个高阶函数的混用:',compound)
输出结果如下
return n! zw
this is origin a: [1, 1, 2, 6, 24]
this is a using sorted [24, 6, 2, 1, 1]
this is a by using sorted : [1, 1, 2, 6, 24]
this is c None
this is a by using list sort [24, 6, 2, 1, 1]
默认字符按照字母顺序排序: ['abcdef', 'b', 'dbc', 'eb', 'gbcd']
按照字符的长度排序 ['b', 'eb', 'dbc', 'gbcd', 'abcdef']
多个高阶函数的混用: [1, 2, 24, 120]
Process finished with exit code 0