zoukankan      html  css  js  c++  java
  • python之路:进阶篇 内置函数

    内置函数
    一、map
    对序列的每一个元素进行操作,最终获得操作后的新序列。

     

    实例:

     1 #!/usr/bin/env  python
     2 # --*--coding:utf-8 --*--
     3 li = [11, 22, 33]
     4 news = map(lambda a: a + 2, li)
     5 print news
     6 li = [22, 33, 44]
     7 l1 = [11, 22, 33]
     8 news = map(lambda a, b: a - b, li, l1)
     9 print news
    10 li = [11, 22, 33]
    11 news = map(lambda a: a * 2, li)
    12 print news
    13 li = [100, 2200, 3300]
    14 news = map(lambda a: a / 2, li)
    15 print news

    实例输出结果:

    1 [13, 24, 35]
    2 [11, 11, 11]
    3 [22, 44, 66]
    4 [50, 1100, 1650]

     序列中的每一个元素经过操作,得出新的序列。两个序列相互操作必须元素相同,如果不同会造成多出的元素与None相互操作,出现错误。

    1 #!/usr/bin/env  python
    2 # --*--coding:utf-8 --*--
    3 li = [22, 33, 44]
    4 l1 = [11, 22]
    5 news = map(lambda a, b: a + b, li, l1)
    6 print news

    报错信息:

    1 Traceback (most recent call last):
    2   File "D:/s11day2/s11day2/test/test.py", line 5, in <module>
    3     news = map(lambda a, b: a + b, li, l1)
    4   File "D:/s11day2/s11day2/test/test.py", line 5, in <lambda>
    5     news = map(lambda a, b: a + b, li, l1)
    6 TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
    二、filter
    筛选序列中符合的元素,把符合条件的元素组成一个新的序列。

    实例:

     1 #!/usr/bin/env  python
     2 # --*--coding:utf-8 --*--
     3 li = [22, 33, 44]
     4 l1 = [11, 22]
     5 news = map(lambda a, b: a + b, li, l1)
     6 print news#!/usr/bin/env  python
     7 # --*--coding:utf-8 --*--
     8 li = [22, 33, 44, 55, 66, 77, 88, 99]
     9 news_list = filter(lambda a: a > 66, li)
    10 print news_list
    11 li = [22, 33, 44, 55, 66, 77, 88, 99]
    12 news_list = filter(lambda a: a > 44 and a < 88 , li)
    13 print news_list

    输出:

    1 [77, 88, 99]
    2 [55, 66, 77]
    三、reduce
    对序列中的所有元素进行累加

     

     1 #!/usr/bin/env  python
     2 # --*--coding:utf-8 --*--
     3 li = [22, 33, 44, 55, 66, 77, 88, 99]
     4 news_list = reduce(lambda a, b: a + b, li)
     5 print news_list
     6 li = [22, 33, 44, 55, 66, 77, 88, 99]
     7 news_list = reduce(lambda a, b: a - b, li)
     8 print news_list
     9 li = [22, 33, 44, 55, 66, 77, 88, 99]
    10 news_list = reduce(lambda a, b: a / b, li)
    11 print news_list
    12 li = [22, 33, 44, 55, 66, 77, 88, 99]
    13 news_list = reduce(lambda a, b: a * b, li)
    14 print news_list
    实例

     输出结果:

    1 484
    2 -440
    3 0
    4 77786550737280
    yield生成器

    对比range和xrange的区别:

    1 >>> print range(10)
    2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    3 >>> print xrange(10)
    4 xrange(10)
    range直接打印出来,而xange在需要的时候迭代循环才会打印出来,
    yield是继续执行上次的操作,如下:
     1 #!/usr/bin/env  python
     2 # --*--coding:utf-8 --*--
     3 def rmange(arg):
     4     seek = 0
     5     while True:
     6         seek = seek + 1
     7         if seek > arg:
     8             return
     9         else:
    10             yield seek
    11 for i in rmange(10):
    12     print i
    第一步,从行到下执行函数(def rmange(arg):),第二步,执行for,第三步,调用上面的函数,进入while循环进行判断到yield,第二次for循环的时候直接不用调用def了,直接进入while循环。yiled就是继续执行上次的操作。可以使用上例进行调试测试。
    装饰器
    作用:具有特殊含义的函数,装饰函数或类。可以在函数执行前或者执行后添加相应的操作。
     1 #!/usr/bin/env  python
     2 # --*--coding:utf-8 --*--
     3 def Before(request,kargs):
     4     print 'before'
     5 def After(request,kargs):
     6     print 'after'
     7 def Filter(before_func,after_func):
     8     def outer(main_func):
     9         def wrapper(request,kargs):
    10             before_result = before_func(request,kargs)
    11             if(before_result != None):
    12                 return before_result;
    13             main_result = main_func(request,kargs)
    14             if(main_result != None):
    15                 return main_result;
    16             after_result = after_func(request,kargs)
    17             if(after_result != None):
    18                 return after_result;
    19         return wrapper
    20     return outer
    21 @Filter(Before, After)
    22 def Index(request,kargs):
    23     print 'index'
    24 if __name__ == '__main__':
    25     Index(1,2)

    执行结果:

    1 before
    2 index
    3 after
    根据python运行规律,从上到下,应该是
    1 before
    2 after
    3 index
    冒泡算法
    作用:根据简单的排序,临近的两个元素进行比较根据要求按顺序排列。
    举例:
    需求:请按照从小到大对列表 [13, 22, 6, 99, 11] 进行排序
    思路:相邻两个值进行比较,将较大的值放在右侧,依次比较!
    1 li = [13, 22, 6, 99, 11]
    2 for m in range(4):     # 等价于 #for m in range(len(li)-1):
    3     if li[m]> li[m+1]:
    4         temp = li[m+1]
    5         li[m+1] = li[m]
    6         li[m] = temp

    第二步

     1 li = [13, 22, 6, 99, 11]
     2 for m in range(4):     # 等价于 #for m in range(len(li)-1):
     3     if li[m]> li[m+1]:
     4         temp = li[m+1]
     5         li[m+1] = li[m]
     6         li[m] = temp
     7 for m in range(3):     # 等价于 #for m in range(len(li)-2):
     8     if li[m]> li[m+1]:
     9         temp = li[m+1]
    10         li[m+1] = li[m]
    11         li[m] = temp
    12 for m in range(2):     # 等价于 #for m in range(len(li)-3):
    13     if li[m]> li[m+1]:
    14         temp = li[m+1]
    15         li[m+1] = li[m]
    16         li[m] = temp
    17 for m in range(1):     # 等价于 #for m in range(len(li)-4):
    18     if li[m]> li[m+1]:
    19         temp = li[m+1]
    20         li[m+1] = li[m]
    21         li[m] = temp
    22 print li

    第三步

    1 li = [13, 22, 6, 99, 11]
    2 for i in range(1,5):    
    3     for m in range(len(li)-i): 
    4         if li[m] > li[m+1]:
    5             temp = li[m+1]
    6             li[m+1] = li[m]
    7             li[m] = temp
    8          print li

    输出结果:

    1 [13, 22, 6, 99, 11]
    2 [13, 6, 22, 99, 11]
    3 [13, 6, 22, 11, 99]
    4 [6, 13, 22, 11, 99]
    5 [6, 13, 11, 22, 99]
    6 [6, 11, 13, 22, 99]
    第一个是原值,后面是五次循环左右两个元素对比根据大小排序。
    递归
    程序分析:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。
    在数学上,费波那契数列是以递归的方法来定义:
    1 F0 = 0     (n=0)
    2 F1 = 1    (n=1)
    3 Fn = F[n-1]+ F[n-2](n=>2)

    程序源代码:

    方法一

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

    方法二

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

     方法三

    如果你需要输出指定个数的斐波那契数列,可以使用以下代码:
    1 def fib(n):
    2     if n == 1:
    3         return [1]
    4     if n == 2:
    5         return [1, 1]
    6     fibs = [1, 1]
    7     for i in range(2, n):
    8         fibs.append(fibs[-1] + fibs[-2])
    9     return fibs

    # 输出前 10 个斐波那契数列 

    print fib(10)

     以上程序运行输出结果为:

    [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
  • 相关阅读:
    各国货币M2增长对比
    Centos6 服务器病毒查杀命令历史
    常见的贷款实际年化利率
    Nginx Rewrite规则
    使用HTML5新特性Mutation Observer实现编辑器的撤销和撤销回退操作
    通过javascript在网页端解压zip文件并查看压缩包内容
    通过javascript在网页端生成zip压缩包并下载
    Plupload上传组件 + javaweb实现上传源码以及DEMO
    chrome 26.0.XXX版本下media query流媒体查询有问题的bug
    epub电子书--目录结构介绍
  • 原文地址:https://www.cnblogs.com/wulaoer/p/5032266.html
Copyright © 2011-2022 走看看