• python 基础语法


    匿名函数

    In [1]: a = lambda x,y:x+y
    
    In [2]: a(1,2)
    Out[2]: 3

     正则表达式

    match如果第一个字符不匹配,则终止;search则是一个个字符里寻找。

    In [3]: import re
    
    In [4]: re.search("hao","da jia hao")
    Out[4]: <_sre.SRE_Match at 0x7eff99911308>
    
    In [5]: re.search("hao","da jia h")
    
    In [6]: re.match("hao","da jia hao")
    
    In [7]: re.match("da","da jia hao")
    Out[7]: <_sre.SRE_Match at 0x7eff999115e0>

     小括号中写上要输出的内容,结果以元组输出

    In [10]: re.match("[0-9]{3}(go)","123go").groups()
    Out[10]: ('go',)

    单独输出

    In [14]: re.match("[0-9]{3}(go)","123go").group(0)
    Out[14]: '123go'
    
    In [15]: re.match("[0-9]{3}(go)","123go").group(1)
    Out[15]: 'go'
    In [29]: re.search("(go)","just go go go !").group(1)
    Out[29]: 'go'

     模块导入导出

    import module

    from module import m1,m2,m3

    import module_name as m4

    通过sys.path可以查找python从目录中查找模块的顺序

    In [31]: import sys
    
    In [32]: sys.path
    Out[32]: 
    ['',
     '/usr/bin',
     '/usr/lib64/python27.zip',
     '/usr/lib64/python2.7',
     '/usr/lib64/python2.7/plat-linux2',
     '/usr/lib64/python2.7/lib-tk',
     '/usr/lib64/python2.7/lib-old',
     '/usr/lib64/python2.7/lib-dynload',
     '/usr/lib64/python2.7/site-packages',
     '/usr/lib64/python2.7/site-packages/mysqlclient-1.3.13-py2.7-linux-x86_64.egg',
     '/usr/lib/python2.7/site-packages',
     '/usr/lib/python2.7/site-packages/setuptools-41.0.1-py2.7.egg',
     '/usr/lib/python2.7/site-packages/IPython/extensions',
     '/root/.ipython']

    第一个目录为空,表示当前目录,即优先从当前目录查找

    然后从/usr/bin目录中查找是否存在所导入的模块,依次排查剩下的所有目录

    as通常用于同一文件中从不同模块引入的方法重名的时候,或者换一个更贴切业务含义的名字

    In [34]: from re import search as sea
    
    In [35]: sea("ka","wa ka ka ")
    Out[35]: <_sre.SRE_Match at 0x7eff98800cc8>

    迭代器

    可以通过next()函数获取其下一个值,直到无值可返回

    In [1]: aa = [1,2,3]
    
    In [2]: a1 = iter(aa)
    
    In [3]: ?a1
    Type:        listiterator
    String form: <listiterator object at 0x7fe0773aef90>
    Docstring:   <no docstring>
    
    In [4]: a1.next()
    Out[4]: 1

    优点,一次读取一个资源,占用资源少;即不会一次把资源全读出来,是一次读取一个

    生成器

    In [10]: def gen(n):
        ...:     yield n+2
        ...:     
    In [12]: aa = gen(1)
    
    In [13]: ?aa
    Type:        generator
    String form: <generator object gen at 0x7fe077396730>
    Docstring:   <no docstring>
    In [14]: aa.next()
    Out[14]: 3

    生成器本质上是迭代器

    列表解析

    In [16]: aa = [1,2,3,4,5,6]
    
    In [17]: [item+2 for item in aa]
    Out[17]: [3, 4, 5, 6, 7, 8]

    生成器表达式

    In [18]: (item +2 for item in aa)
    Out[18]: <generator object <genexpr> at 0x7fe0773a00a0>
    
    In [19]: bb = (item +2 for item in aa)
    
    In [20]: ?bb
    Type:        generator
    String form: <generator object <genexpr> at 0x7fe077396b40>
    Docstring:   <no docstring>
    
    In [21]: bb.next()
    Out[21]: 3
    
    In [22]: bb.next()
    Out[22]: 4

    装饰器

    In [40]: def addLogging(func):
        ...:     def log(*args, **kwargs):
        ...:         print "%s is running"%func.__name__
        ...:         return func(*args)
        ...:     return log
        ...: 
    
    In [41]: def aa():
        ...:     print 123
        ...:     
    
    In [42]: aa = addLogging(aa)
    
    In [43]: aa()
    aa is running
    123
    
    In [44]: @addLogging
        ...: def bb():
        ...:     print 111
        ...:     
    
    In [45]: bb()
    bb is running
    111

     在原来的方法外面包装一层处理,就像商品的包装一样;有参数传入和修饰符@两种方式

  • 相关阅读:
    Excel对象
    使用C#和Excel进行报表开发(6)
    使用C#和Excel进行报表开发(1)
    c# excel一个小例子
    HDU1226 BFS
    HDU2145 SPFA
    HDU1229
    HDU1535 spfa
    HDU1230
    HDU3986 SPFA
  • 原文地址:https://www.cnblogs.com/perfei/p/11070601.html
Copyright © 2020-2023  润新知