python内置函数

lambda匿名函数

func = lambda x :x+1
print(func(1))

-------执行结果-------
2
aa = [1,2,3,4]
def map_test(func,num):
    ret = []
    for i in num:
        res = func(i)
        ret.append(res)
    return ret

print(map_test(lambda  x :x*x,aa))

-------执行结果-------
[1,4,9,16]

map函数

aa = [1,2,3,4,5]
print('内置函数map',map(lambda x :x+1,aa))
for i in map(lambda x :x+1,aa):
    print(i)
print(list(map(lambda x :x+1,aa)))

-------执行结果-------
内置函数map <map object at 0x00000000026FA1D0>
2
3
4
5
6
[2, 3, 4, 5, 6]

filter函数

li = ['a_123','lemon','luoluo','a_321']
listall = filter(lambda x:not x.startswith('a'),li)
print(list(listall))

-------执行结果-------
['lemon', 'luoluo']

  

reduce函数

from functools import reduce
num = [1,2,3,4,5]
print(reduce(lambda x,y:x+y,num))

-------执行结果-------
15

  

其他内置函数

  

print(abs(-12580)) #输出绝对值

print(all([1,2,3,4,5,'123'])) #判断列表当中是否全是真值

print(any([1,2,3,]))  #判断列表中是否有一个真值

print(bool(1))  #判断布尔值  , '空,0,None'为False

print(chr(97)) #打印ascii码表的对应关系 ----》 a

print(divmod(100,3)) #得出商和余数 -----》(33,1)

print(pow(2,8))  #输出2的8次方

  

eval函数

dic = "{'name':'lemon'}"
print(type(dic))
print(eval(dic))  #将字符串转换成字典
print(type(eval(dic)))
abc = '1+2+3'
print(eval(abc)) #将字符串的运算式计算出来
-------执行结果------- <class 'str'> {'name': 'lemon'} <class 'dict'> 6

  

zip函数

zip将两个可迭代的对象缝合器来

test = zip(('a','b','c'),[1,2,3,4,6])
li = list(test)
print(li)

-------执行结果-------
[('a', 1), ('b', 2), ('c', 3)]

  

sorted函数

对可迭代对象进行排序

li = [1,2,3,4,5,6,0,2134,123,2]
print(sorted(li))

-------执行结果-------
[0, 1, 2, 2, 3, 4, 5, 6, 123, 2134]

  

原文地址:https://www.cnblogs.com/lemonbk/p/10665578.html