会飞的蝌蚪君

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

                                                            python的内置函数一览:

 

#内置函数在  __builtins__ 里面

框框必会,五星重点:

 

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

 bytes  bytearray

 

chr     ord

 

any   all      ascii

# chr & ord 的应用

 

eval运用:

 

 

 

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

以下的import实现相同功能!!!!!

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

sorted 排序

#以下两种方法相同:

 

 

# zip 函数操作:

https://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html

 

 

 

=====================================================================

高阶函数 map/reduce

#map

1 def f(x):
2    return x * x
3 
4 r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
5 print(list(r))
6 #[1, 4, 9, 16, 25, 36, 49, 64, 81]

#reduce

1 from functools import reduce
2 def add(x, y):
3     return x + y
4 
5 r = reduce(add, [1, 3, 5, 7, 9])
6 print(r)

实例:

 1 def normalize(name):
 2     name_update = []
 3     for i in name:
 4         if i == name[0]:
 5            r = name.capitalize()
 6            name_update.append(r)
 7         else:
 8            s = i.lower()
 9            name_update.append(s)
10         return name_update[0]
11 
12 
13 L1 = ['adam', 'LISA', 'barT']
14 L2 = list(map(normalize, L1))
15 print(L2)   #将L1里面的名称首字母大写,其他字母小写,并返回。

 

 1 def proud(a):
 2     def product(x,y):
 3         return x * y
 4     return reduce(product,a)
 5 
 6 print('3 * 5 * 7 * 9 =', proud([3, 5, 7, 9]))
 7 if proud([3, 5, 7, 9]) == 945:
 8     print('测试成功!')
 9 else:
10     print('测试失败!')        

 

posted on 2017-11-15 19:05  会飞的蝌蚪  阅读(148)  评论(0编辑  收藏  举报