内置函数

 

 

abs

#abs() 函数返回数字的绝对值。

print (abs(-1))
1
View Code

 

 

filter筛选

filter(function,iderable)
我们自己筛选方式,把需要筛选的对象给他,filter就能帮我们筛选

names=['a_sb','b_sb','c','d','yeee']
res=filter(lambda name:name.endswith('sb'),names)
print(list(res))

['a_sb', 'b_sb']
View Code

 

 

map映射

#对可迭代对象中的每一个元素进行映射,分别去执行function


def func(x):
    return x*x
res=map(func,[1,2,3,4,5,6,7,8,9])
print(list(res))

------------------------------------------------

print(list(map(lambda x:x*x,[1,2,3,4,5])))

------------------------------------------------
res=map(str,[1,2,3,4,5])
print(list(res))


[1, 4, 9, 16, 25, 36, 49, 64, 81]
[1, 4, 9, 16, 25]
['1', '2', '3', '4', '5']
View Code

 

 

sorted排序

sorted(iderable,key,reverse)   #支持倒序

把可迭代对象中的每一个元素取出来,交给后边的key,计算出数字,作为当前对象的权重,根据权重排序

li=['123','三国演义','1','七夕']

def func(x):
    return len(x)

res=sorted(li,key=func)
print(res)

['1', '七夕', '123', '三国演义']
---------------------------------------------------

res=sorted(li,key=lambda li:len(li),reverse=True)
print(res)
 
['三国演义', '123', '七夕', '1']
View Code

 

posted @ 2019-08-05 15:15  pdun  阅读(164)  评论(0编辑  收藏  举报