# 高阶函数map | reduce | filter | sorted
# map(fn, List)
def newList(x):
return x * x
list_c = map(newList, [1,2,3,4,5,6])
print(list(list_c))
# [1, 4, 9, 16, 25, 36]
# filter接收一个函数和一个序列
# 把传入的函数依次作用于每个元素,然后根据返回值是 True 还是 False 决定保留还是丢弃该元素
def isOdd(x):
return x % 2 == 0
list_d = filter(isOdd, [1,2,3,4,5,6])
print(list(list_d))
# [2, 4, 6]
# sorted(list, key) 接收一个 key 函数来实现自定义的排序
list_e = [2,4,5,6,2,7,2,44,50,60]
print(sorted(list_e))
# [2, 2, 2, 4, 5, 6, 7, 44, 50, 60]
# 在排序比较时,先把每个字符串都转成小写,再按字母顺序排,但原字符串本身不变,即不区分大小写的字母排序
list_f = ['bob', 'about', 'Zoo', 'Credit']
print(sorted(list_f, key=str.lower))
# ['about', 'bob', 'Credit', 'Zoo']