码迷,mamicode.com
首页 > 编程语言 > 详细

python进阶 内置函数

时间:2015-11-21 07:15:33      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:python

内置函数:

一、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, 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














本文出自 “吴老二” 博客,请务必保留此出处http://9827789.blog.51cto.com/9817789/1715278

python进阶 内置函数

标签:python

原文地址:http://9827789.blog.51cto.com/9817789/1715278

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!