您的位置:首页 > 编程语言 > Python开发

python进阶 内置函数

2015-11-20 23:51 603 查看
内置函数:
一、map
对序列的每一个元素进行操作,最终获得操作后的新序列。



实例:
#!/usr/bin/env  python
# --*--coding:utf-8 --*--
li = [11, 22, 33]
news = map(lambda a: a + 2, li)
print news
li = [22, 33, 44]
l1 = [11, 22, 33]
news = map(lambda a, b: a - b, li, l1)
print news
li = [11, 22, 33]
news = map(lambda a: a * 2, li)
print news
li = [100, 2200, 3300]
news = map(lambda a: a / 2, li)
print news


实例输出结果:
[13, 24, 35]
[11, 11, 11]
[22, 44, 66]
[50, 1100, 1650]
序列中的每一个元素经过操作,得出新的序列。两个序列相互操作必须元素相同,如果不同会造成多出的元素与None相互操作,出现错误。

#!/usr/bin/env  python
# --*--coding:utf-8 --*--
li = [22, 33, 44]
l1 = [11, 22]
news = map(lambda a, b: a + b, li, l1)
print news
报错信息:
Traceback (most recent call last):
File "D:/s11day2/s11day2/test/test.py", line 5, in <module>
news = map(lambda a, b: a + b, li, l1)
File "D:/s11day2/s11day2/test/test.py", line 5, in <lambda>
news = map(lambda a, b: a + b, li, l1)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
二、filter
筛选序列中符合的元素,把符合条件的元素组成一个新的序列。




实例:

#!/usr/bin/env  python
# --*--coding:utf-8 --*--
li = [22, 33, 44]
l1 = [11, 22]
news = map(lambda a, b: a + b, li, l1)
print news#!/usr/bin/env  python
# --*--coding:utf-8 --*--
li = [22, 33, 44, 55, 66, 77, 88, 99]
news_list = filter(lambda a: a > 66, li)
print news_list
li = [22, 33, 44, 55, 66, 77, 88, 99]
news_list = filter(lambda a: a > 44 and a < 88 , li)
print news_list
输出:
[77, 88, 99]
[55, 66, 77]
三、reduce
对序列中的所有元素进行累加




实例:

#!/usr/bin/env  python
# --*--coding:utf-8 --*--
li = [22, 33, 44, 55, 66, 77, 88, 99]
news_list = reduce(lambda a, b: a + b, li)
print news_list
li = [22, 33, 44, 55, 66, 77, 88, 99]
news_list = reduce(lambda a, b: a - b, li)
print news_list
li = [22, 33, 44, 55, 66, 77, 88, 99]
news_list = reduce(lambda a, b: a / b, li)
print news_list
li = [22, 33, 44, 55, 66, 77, 88, 99]
news_list = reduce(lambda a, b: a * b, li)
print news_list
输出结果:
484
-440
0
77786550737280
yield生成器
对比range和xrange的区别:

>>> print range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print xrange(10)
xrange(10)

range直接打印出来,而xange在需要的时候迭代循环才会打印出来,
yield是继续执行上次的操作,如下:

#!/usr/bin/env  python
# --*--coding:utf-8 --*--
def rmange(arg):
seek = 0
while True:
seek = seek + 1
if seek > arg:
return
else:
yield seek
for i in rmange(10):
print i
第一步,从行到下执行函数(def rmange(arg):),第二步,执行for,第三步,调用上面的函数,进入while循环进行判断到yield,第二次for循环的时候直接不用调用def了,直接进入while循环。yiled就是继续执行上次的操作。可以使用上例进行调试测试。
装饰器
作用:具有特殊含义的函数,装饰函数或类。可以在函数执行前或者执行后添加相应的操作。

#!/usr/bin/env  python
# --*--coding:utf-8 --*--
def Before(request,kargs):
print 'before'
def After(request,kargs):
print 'after'
def Filter(before_func,after_func):
def outer(main_func):
def wrapper(request,kargs):
before_result = before_func(request,kargs)
if(before_result != None):
return before_result;
main_result = main_func(request,kargs)
if(main_result != None):
return main_result;
after_result = after_func(request,kargs)
if(after_result != None):
return after_result;
return wrapper
return outer
@Filter(Before, After)
def Index(request,kargs):
print 'index'
if __name__ == '__main__':
Index(1,2)
执行结果:
before
index
after
根据python运行规律,从上到下,应该是
before
after
index
冒泡算法
作用:根据简单的排序,临近的两个元素进行比较根据要求按顺序排列。
举例:

需求:请按照从小到大对列表 [13, 22, 6, 99, 11] 进行排序
思路:相邻两个值进行比较,将较大的值放在右侧,依次比较!
li = [13, 22, 6, 99, 11]
for m in range(4):     # 等价于 #for m in range(len(li)-1):
if li[m]> li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
第二步
li = [13, 22, 6, 99, 11]
for m in range(4):     # 等价于 #for m in range(len(li)-1):
if li[m]> li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
for m in range(3):     # 等价于 #for m in range(len(li)-2):
if li[m]> li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
for m in range(2):     # 等价于 #for m in range(len(li)-3):
if li[m]> li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
for m in range(1):     # 等价于 #for m in range(len(li)-4):
if li[m]> li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
第三步
li = [13, 22, 6, 99, 11]
for i in range(1,5):
for m in range(len(li)-i):
if li[m] > li[m+1]:
temp = li[m+1]
li[m+1] = li[m]
li[m] = temp
print li
输出结果:
[13, 22, 6, 99, 11]
[13, 6, 22, 99, 11]
[13, 6, 22, 11, 99]
[6, 13, 22, 11, 99]
[6, 13, 11, 22, 99]
[6, 11, 13, 22, 99]
第一个是原值,后面是五次循环左右两个元素对比根据大小排序。
递归
程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。在数学上,费波那契数列是以递归的方法来定义:
F0 = 0     (n=0)
F1 = 1    (n=1)
Fn = F[n-1]+ F[n-2](n=>2)
程序源代码:

方法一

#!/usr/bin/python
# -*- coding: UTF-8 -*-
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
# 输出了第10个斐波那契数列
print fib(10)

方法二

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 使用递归
def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2)
# 输出了第10个斐波那契数列
print fib(10)
以上实例输出了第10个斐波那契数列,结果为:
55

方法三

如果你需要输出指定个数的斐波那契数列,可以使用以下代码:
def fib(n):
if n == 1:
return [1]
if n == 2:
return [1, 1]
fibs = [1, 1]
for i in range(2, n):
fibs.append(fibs[-1] + fibs[-2])
return fibs
# 输出前 10 个斐波那契数列
print fib(10)
以上程序运行输出结果为:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python