05 python s12 day4 迭代器原理及使用
什么是迭代:
可以直接作用于for循环的对象统称为可迭代对象(Iterable)。*
可以被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator)。
所有的Iterable均可以通过内置函数iter()来转变为Iterator。
对迭代器来讲,有一个next()就够了。在你使用for 和 in 语句时,程序就会自动调用即将被处理的对象的迭代器对象,然后使用它的next()方法,直到监测到一个StopIteration异常。
Python随笔(四)、python基础

#!usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: 迭代器.py
@time: 2018/01/01
"""
names = iter(['alex','xsb','zsb','dsb'])
print(names)
print(names.next())
print(names.next())
print(names.next())
print(names.next())
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2018-01-01/迭代器.py
<list_iterator object at 0x00000000026DC470>
alex
xsb
zsb
dsb

06 python s12 day4 生成器的使用
def cash_money(amount):
while amount >0:
amount -=100
yield 100
print("又来取钱了?")

atm = cash_money(600)
print(type(atm))
print(atm.next())
print(atm.next())
print("叫个大保健")
print(atm.next())
print(atm.next())
print(atm.next())
print(atm.next())
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2018-01-01/迭代器.py
<class 'generator'>
100
又来取钱了?
100
叫个大保健
又来取钱了?
100
又来取钱了?
100
又来取钱了?
100
又来取钱了?
100

07 python s12 day4 使用yield实现单线程中的异步并发效果
注意:yield和break的区别
#!usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: yield迭代器.py
@time: 2018/01/01
"""
import time
def consumer(name):
print("%s 准备吃包子了!" %name)
while True:
baozi = yield
print("包子[%s]来了,被[%s]吃了" %(baozi,name))

def producer(name):
c = consumer('A')
c2 = consumer('B')
c.next()
c2.next()
print("老子准备开始做包子了!")
for i in range(10):
time.sleep(1)
print("做了2个包子!")
c.send('Tenglan')
c2.send(i)

producer("alex")
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2018-01-01/yield迭代器.py
A 准备吃包子了!
B 准备吃包子了!
老子准备开始做包子了!
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[0]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[1]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[2]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[3]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[4]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[5]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[6]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[7]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[8]来了,被[B]吃了
做了2个包子!
包子[Tenglan]来了,被[A]吃了
包子[9]来了,被[B]吃了

08 python s12 day4 装饰器原理介绍和基本实现
http://www.cnblogs.com/wupeiqi/articles/4980620.html
2、需求来了
初创公司有N个业务部门,1个基础平台部门,基础平台负责提供底层的功能,如:数据库操作、redis调用、监控API等功能。业务部门使用基础功能时,只需调用基础平台提供的功能即可。如下:
############### 基础平台提供的功能如下 ###############

def f1():
print 'f1'

def f2():
print 'f2'

def f3():
print 'f3'

def f4():
print 'f4'

############### 业务部门A 调用基础平台提供的功能 ###############

f1()
f2()
f3()
f4()

############### 业务部门B 调用基础平台提供的功能 ###############

f1()
f2()
f3()
f4()

作用,给已经存在的功能扩展新的功能

09 python s12 day4 装饰器实现
装饰器代码:
#!usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: 装饰器.py
@time: 2018/01/01
"""
#第一种方式
'''
def login(func):
print("passwd user verification.....")
return func
'''
return None
'''
def home(name):
print("Welcome [%s] to home page" % name )br/>@login
def tv(name):
print("Welcome [%s] to TV page" % name )
def moive(name):
print("Welcome [%s] to moive page" % name )

#tv = login(tv)
tv("Alex")
'''
#第二种方式:
'''
def login(func):
print("passwd user verification.....")
return func
def home(name):
print("Welcome [%s] to home page" % name )
def tv():
print("Welcome [%s] to TV page" )
def moive(name):
print("Welcome [%s] to moive page" % name )

tv = login(tv)
tv()
'''
#第三种方式:
def login(func):
def inner(arg):
print("passwd user verification.....")
func(arg)
return inner

def home(name):
print("Welcome [%s] to home page" % name )
br/>@login
def tv(name):
print("Welcome [%s] to TV page" % name )
def moive(name):
print("Welcome [%s] to moive page" % name )

tv("Alex")
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2018-01-01/装饰器.py
passwd user verification.....
Welcome [Alex] to TV page

10 python s12 day4 实现带参数的复杂装饰器
def login(func):
def inner(arg):
print("passwd user verification.....")
func(arg)
return inner

def home(name):
print("Welcome [%s] to home page" % name )
br/>@login
def tv(name):
print("Welcome [%s] to TV page" % name )
br/>@login
def moive(name):
print("Welcome [%s] to moive page" % name )

tv("Alex")
moive("Alex")
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2018-01-01/装饰器.py
passwd user verification.....
Welcome [Alex] to TV page
passwd user verification.....
Welcome [Alex] to moive page

装饰器多参数及返回值:
def login(func):
def inner(*args,*kwargs):
print("passwd user verification.....")
return func(
args,**kwargs)
return inner

def home(name):
print("Welcome [%s] to home page" % name )
br/>@login
def tv(name,passwd="123"):
print("Welcome [%s] to TV page" % name )
return 4
br/>@login
def moive(name):
print("Welcome [%s] to moive page" % name )

t = tv("Alex",passwd="123")
print(t)
moive("Alex")
返回结果:
E:\Python36\python.exe C:/Users/Administrator/PycharmProjects/2018-01-01/装饰器.py
passwd user verification.....
Welcome [Alex] to TV page
4
passwd user verification.....
Welcome [Alex] to moive page

11 python s12 day4 递归原理及实现
#!usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: 递归.py
@time: 2018/01/01
"""
'''
def digui(n):
sum = 0
if n<=0:
return 1
else:
return n+digui(n-1)

print(digui(30))
'''
def calc(n):
print(n)
if n/2 > 1:
res = calc(n/2)
return res

calc(100)

def calc(n):
print(n)
if n/2 > 1:
res = calc(n/2)
print('res:',res)
print('N:',n)
return n

calc(10)

12 python s12 day4 通过递归实现斐波那契数列
#!usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: 斐波那契数列.py
@time: 2018/01/01
"""
def func(arg1,arg2,stop):
if arg1 == 0:
print(arg1,arg2)
arg3 = arg1 + arg2
print(arg3)
if arg3< stop:
func(arg2,arg3,stop)

func(0,1,100)

13 python s12 day4 算法基础之二分查找
在庞大的数据中找一个数是否在其中,比如查找65535是否在600000中。
#/usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: binary_search.py
@time: 2018/06/30
data = list(range(1,600,3))
print (data)
"""

def binary_search(data_source,find_n):
mid = int(len(data_source)/2)
if len(data_source) >1:
if data_source[mid] > find_n:
print("data in left of [%s]" % data_source[mid])
binary_search(data_source[:mid],find_n)
elif data_source[mid] < find_n:
print("data in right of [%s]" % data_source[mid])
binary_search(data_source[mid:],find_n)
else:
print("find find_s",data_source[mid])

else:
    print("cannot find......")

if name=='main':
data = list(range(1,600000))
binary_search(data,65535)

结果输出:
E:\Python36\python.exe G:/PycharmProjects/basic/binary_search.py
data in left of [300000]
data in left of [150000]
data in left of [75000]
data in right of [37500]
data in right of [56250]
data in left of [65625]
data in right of [60937]
data in right of [63281]
data in right of [64453]
data in right of [65039]
data in right of [65332]
data in right of [65478]
data in left of [65551]
data in right of [65514]
data in right of [65532]
data in left of [65541]
data in left of [65536]
data in right of [65534]
find find_s 65535

14 python s12 day4 算法基础之2维数组90度旋转
#/usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: erweiarr.py
@time: 2018/07/01
"""
#二维数组
a = [[col for col in range(4)] for row in range(4)]
for i in a:
print(i)

结果:
E:\Python36\python.exe G:/PycharmProjects/basic/erweiarr.py
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]

Process finished with exit code 0

2维数组90度旋转
#/usr/bin/env python
#-- coding:utf-8 _-
"""
@author:Administrator
@file: erweiarr.py
@time: 2018/07/01
"""
data = [[col for col in range(4)] for row in range(4)]
for row in data:
print(row)
print("==============================")
'''
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]

[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
'''

for r_index,row in enumerate(data):
for c_index in range(r_index,len(row)):
tmp = data[c_index][r_index]
data[c_index][r_index] = row[c_index]
data[r_index][c_index] = tmp
print('================================')
for r in data:
print(r)

输出结果:
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]

================================
[0, 0, 0, 0]
[1, 1, 2, 3]
[2, 1, 2, 3]
[3, 1, 2, 3]

[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 3]
[3, 3, 2, 3]

[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]

[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]

15 python s12 day4 正则表达式基础及计算器作业思路及要求
import re

m = re.match("abc","abcdwef")
print(m)

输出结果:
<_sre.SRE_Match object; span=(0, 3), match='abc'>

import re

m = re.match("abc","abcdwef")
m = re.match("[0-9]","07876598jkfjkdshfjsk")
m = re.match("[0-9]{0,10}","07876598jkfjkdshfjsk")
m = re.findall("[0-9]{0,10}","07876598jkfjkd54645shfjsk")
m = re.findall("[a-zA-Z]{1,10}","07876598jkfjkd54645shfjsk")
M = re.findall(".*","07876598jkfjkd54645shfjsk")
m = re.findall(".+","07876598jkfjkd54645shfjsk")
m = re.findall("\S","07876598jkfjkd54645shfjsk")
m = re.search("\d+","07876598jkfjkd54645shfjsk")
m = re.sub("\d+","|","sdfdsfds75_45.6 4a~bc6@def",count=2)
if m:
print(m)