05 python s12 day4 迭代器原理及使用
什麼是迭代:
能夠直接做用於for循環的對象統稱爲可迭代對象(Iterable)。*
能夠被next()函數調用並不斷返回下一個值的對象稱爲迭代器(Iterator)。
全部的Iterable都可以經過內置函數iter()來轉變爲Iterator。
對迭代器來說,有一個next()就夠了。在你使用for 和 in 語句時,程序就會自動調用即將被處理的對象的迭代器對象,而後使用它的next()方法,直到監測到一個StopIteration異常。
html
#!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
dsbpython
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
又來取錢了?
100redis
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
二、需求來了
初創公司有N個業務部門,1個基礎平臺部門,基礎平臺負責提供底層的功能,如:數據庫操做、redis調用、監控API等功能。業務部門使用基礎功能時,只需調用基礎平臺提供的功能便可。以下:
############### 基礎平臺提供的功能以下 ###############併發
def f1():
print 'f1'異步
def f2():
print 'f2'ide
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 4br/>@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, 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)