zoukankan      html  css  js  c++  java
  • python课程2


    一 魔术方法

    __getitem__ 给函数使用func[n]的方法
    __contains__方法 就是函数实现in方法的魔术方法
    __repr__ 通过得到一个对象的字符串表示形式的
    __str__ 是调用str()实现
    __add__ 实现+算术运算符
    __mul__ 实现*算术运算符


    二序列组成的数组
    2.1 python自带类型分类
    容器序列
    list tuple collections.deque
    指的存放任意对象的应用

    扁平序列
    str bytes bytearray memoryview array.array
    存放的指,还不是引用,扁平序列就是一段连续内存空间

    可变序列
    list bytearray array.array collections.deque memoryview
    不可变序列
    tuple str bytes


    2.2 列表表达式
    python 2 python3 区别,3有局部作用域 但是2没有 所以列表表达式在2中有变量泄露

    用列表表达式做笛卡尔积

    1 colos = ["bb","as"]
    2 sizes = [1,3,4]
    3 
    4 aa = [(colo,size) for colo in colos for size in sizes]
    5 print(aa)

    字符串变成asic码的列表

    tests="sfafasfzx"
    print([ord(test)for test in tests])
    View Code

    2.3 生成器表达式
    用生成器来初始化数组,元祖等其他序列类型,因为生成器遵守迭代器的协议

    2.4 元组
    元祖不仅仅是不可变列表,其他提供功能
    1 元素的总数,元素的位置

    拆包 每个类型都有拆包解包功能
    fuck,fuckyou ,_ = (1,3,4,5,6,7)
    fuck,fuckyou ,*otherfuck = (1,3,4,5,6,7)


    str的format用法
    同时指定宽度和精度的一般形式是 '[<>^]?width[,]?(.digits)?' , 其中 width 和 digits 为整数,?代表可选部分。 同样的格式也被用在字符串的 format() 方法中。比如:
    1 0.2f 保留2位小数
    2 >10.1f 左边保留10个字符长度
    3 <10.1f 右边保留10个字符长度
    4 ^10.1f 居中
    当跟着str后,即''.format时候,需要在前面加上: 号

    >>> 'The value is {:0,.2f}'.format(x)
    'The value is 1,234.57'
    >>>
    
    >>> print('{:15}|{:^9}|{:^9}'.format('','lat.','long.'))
    
                | lat. | long.    

    当值不存在时候处理

     1 format 可以用**传递字典,*传递list进formart

    2 也可以使用foramt_map传送字典

    3 当class对象 传入时候需要用vars进行转换

    4__missing__处理当指不存在时候进行处理

    class Infi:
        def __init__(self,name,n):
            self.name = name
            self.n = n
        
    
    ba = Infi('lqm',11)
    print(vars(ba))
    s = '{name} has {n} messages'
    print(s.format_map(vars(ba)))

    print(s.format(**vars(ba)))
     
    1 {'name': 'lqm', 'n': 11}
    2 lqm has 11 messages
    3 {'name': 'lqm', 'n': None}
    4 lqm has None messages
    result

    具名元祖

    namedtuple 可以实现带字段名的元祖,实际内存消耗和元祖差不多
    from collections import namedtuple
    
    City = namedtuple('city','name country pop corrd')
    tokyo = City('Tokyo','jp',36.33,(11,22))
    print(tokyo._fields)
    dadi_data = ("ncr","in",21.95,223)
    dadi = City._make(dadi_data)
    print(dadi._asdict())
    
    结果
    ('name', 'country', 'pop', 'corrd')
    OrderedDict([('name', 'ncr'), ('country', 'in'), ('pop', 21.95), ('corrd', 223)])

    注意方法 ._fields 获取类的字段名

                     ._make 生成字段数据

                    ._asdict() 显示具名元祖数据

                     每个字段可以存取不同的类型

    切片

    对切片赋值时候,右边必须是可迭代的对象,就算是一个指也需要

    l = list(range(10))
    l[2:5] = [100]
    print(l)
    
    
    result:
    [0, 1, 100, 5, 6, 7, 8, 9]

    array 数组

    数组只能存放int和float类型

    Type codeC TypePython TypeMinimum size in bytesNotes
    'b' signed char int 1  
    'B' unsigned char int 1  
    'u' Py_UNICODE Unicode character 2 (1)
    'h' signed short int 2  
    'H' unsigned short int 2  
    'i' signed int int 2  
    'I' unsigned int int 2  
    'l' signed long int 4  
    'L' unsigned long int 4  
    'q' signed long long int 8 (2)
    'Q' unsigned long long int 8 (2)
    'f' float float 4  
    'd' double float 8  
    from array import array
    floats = array('d',[1,3,4,5])

     array有更快的读写文件方法 frombytes tofile

    数组支持深度拷贝,而list不支持

  • 相关阅读:
    Qt CheckBox选中与未选中的使用
    Qt Excel表格宽高的设置
    VTK三维重建 使用VTK读取DICOM,并动态输出
    VC++ VTK 读取序列CT图片三维重建
    VTK 读取序列图像
    Qt QWidget提升QVTKWidget
    ITK 介绍
    Qt QMainWindow中利用多个QDockWidget构成标签页tab || tabifyDockWidget
    Qt QDockWidget属性 setFeatures、setAllowedAreas
    Qt Designer中toolBar的allowedAreas属性
  • 原文地址:https://www.cnblogs.com/liuqimin/p/8783803.html
Copyright © 2011-2022 走看看