python -高阶函数

'''map(fun,[1,23,4])函数第一个参数是函数,第二个参数是集合,
运行的时候,会把集合中的每一个元素放到前面的函数当中,并且函数有返回值,形成一个新的集合需要
list 或者tuple

'''

def fun(x):
return x**2
list1 = list(map(fun,[1,2,3]))
list2 = list(map(lambda x:x**2 ,[1,2,3]))
str1 = list(map(str,(1,2,4)))
print(''.join(list2))
print(str1)



'''sorted(集合,key=fun) 顺序不能反了'''

dict1 = {'a':1,'b':2,'d':4,'c':3}
'''把字典按值进行排序'''
def fun1(x):
return x[1]


print(sorted(dict1.items(),key=fun1))

猜你喜欢

转载自www.cnblogs.com/tarzen213/p/11093490.html