设为首页 收藏本站
查看: 1514|回复: 0

[经验分享] Python随笔(四)、python基础

[复制链接]

尚未签到

发表于 2018-8-4 09:31:24 | 显示全部楼层 |阅读模式
  05 python s12 day4  迭代器原理及使用
  什么是迭代:
  可以直接作用于for循环的对象统称为可迭代对象(Iterable)。*
  可以被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator)。
  所有的Iterable均可以通过内置函数iter()来转变为Iterator。
  对迭代器来讲,有一个next()就够了。在你使用for 和 in 语句时,程序就会自动调用即将被处理的对象的迭代器对象,然后使用它的next()方法,直到监测到一个StopIteration异常。
DSC0000.jpg

  #!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(&quot;又来取钱了?&quot;)
  atm = cash_money(600)
  print(type(atm))
  print(atm.next())
  print(atm.next())
  print(&quot;叫个大保健&quot;)
  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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: yield迭代器.py
  @time: 2018/01/01
  &quot;&quot;&quot;
  import time
  def consumer(name):
  print(&quot;%s 准备吃包子了!&quot; %name)
  while True:
  baozi = yield
  print(&quot;包子[%s]来了,被[%s]吃了&quot; %(baozi,name))
  def producer(name):
  c = consumer('A')
  c2 = consumer('B')
  c.next()
  c2.next()
  print(&quot;老子准备开始做包子了!&quot;)
  for i in range(10):
  time.sleep(1)
  print(&quot;做了2个包子!&quot;)
  c.send('Tenglan')
  c2.send(i)
  producer(&quot;alex&quot;)
  返回结果:
  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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: 装饰器.py
  @time: 2018/01/01
  &quot;&quot;&quot;
  #第一种方式
  '''
  def login(func):
  print(&quot;passwd user verification.....&quot;)
  return func
  '''
  return None
  '''
  def home(name):
  print(&quot;Welcome [%s] to home page&quot; % name )@loginbr/>@login
def tv(name):
  print(&quot;Welcome [%s] to TV page&quot; % name  )
  def moive(name):
  print(&quot;Welcome [%s] to moive page&quot; % name )
  tv(&quot;Alex&quot;)
  返回结果:
  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(&quot;passwd user verification.....&quot;)
  func(arg)
  return inner
  def home(name):
  print(&quot;Welcome [%s] to home page&quot; % name )@loginbr/>@login
def moive(name):
  print(&quot;Welcome [%s] to moive page&quot; % name )
  tv(&quot;Alex&quot;)
  moive(&quot;Alex&quot;)
  返回结果:
  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(&quot;passwd user verification.....&quot;)
  return func(args,**kwargs)
  return inner
  def home(name):
  print(&quot;Welcome [%s] to home page&quot; % name )@loginbr/>@login
def moive(name):
  print(&quot;Welcome [%s] to moive page&quot; % name )
  t = tv(&quot;Alex&quot;,passwd=&quot;123&quot;)
  print(t)
  moive(&quot;Alex&quot;)
  返回结果:
  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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: 递归.py
  @time: 2018/01/01
  &quot;&quot;&quot;
  '''
  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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: 斐波那契数列.py
  @time: 2018/01/01
  &quot;&quot;&quot;
  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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: binary_search.py
  @time: 2018/06/30
  data = list(range(1,600,3))
  print (data)
  &quot;&quot;&quot;
  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(&quot;data in left of [%s]&quot; % data_source[mid])
  binary_search(data_source[:mid],find_n)
  elif data_source[mid] < find_n:
  print(&quot;data in right of [%s]&quot; % data_source[mid])
  binary_search(data_source[mid:],find_n)
  else:
  print(&quot;find find_s&quot;,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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: erweiarr.py
  @time: 2018/07/01
  &quot;&quot;&quot;
  #二维数组
  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 _-
  &quot;&quot;&quot;
  @author:Administrator
  @file: erweiarr.py
  @time: 2018/07/01
  &quot;&quot;&quot;
  data = [[col for col in range(4)] for row in range(4)]
  for row in data:
  print(row)
  print(&quot;==============================&quot;)
  '''
  [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(&quot;abc&quot;,&quot;abcdwef&quot;)
  print(m)
  输出结果:
  <_sre.SRE_Match object; span=(0, 3), match='abc'>
  import re
  m = re.match(&quot;abc&quot;,&quot;abcdwef&quot;)
  m = re.match(&quot;[0-9]&quot;,&quot;07876598jkfjkdshfjsk&quot;)
  m = re.match(&quot;[0-9]{0,10}&quot;,&quot;07876598jkfjkdshfjsk&quot;)
  m = re.findall(&quot;[0-9]{0,10}&quot;,&quot;07876598jkfjkd54645shfjsk&quot;)
  m = re.findall(&quot;[a-zA-Z]{1,10}&quot;,&quot;07876598jkfjkd54645shfjsk&quot;)
  M = re.findall(&quot;.*&quot;,&quot;07876598jkfjkd54645shfjsk&quot;)
  m = re.findall(&quot;.+&quot;,&quot;07876598jkfjkd54645shfjsk&quot;)
  m = re.findall(&quot;\S&quot;,&quot;07876598jkfjkd54645shfjsk&quot;)
  m = re.search(&quot;\d+&quot;,&quot;07876598jkfjkd54645shfjsk&quot;)
  m = re.sub(&quot;\d+&quot;,&quot;|&quot;,&quot;sdfdsfds75_45.6  4a~bc6@def&quot;,count=2)
  if m:
  print(m)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-546280-1-1.html 上篇帖子: 关于Python列表中10个最常见的问题 下篇帖子: Python自动化运维之异常处理
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表