zoukankan      html  css  js  c++  java
  • python笔记3

    1 集合
    1 每个元素不可变
    2 无序
    3 元素内不可重复

    功能点 1 去除重复
    f_l = [1,3,1,1,56,7,8,9,0]
    print(set(f_l))

    可变类型》不可hash
    不可变类型
    >可hash

    s = {1,3,"1s",(1,3)}

    ['and', 'class', 'contains', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'iand', 'init', 'ior', 'isub', 'iter', 'ixor', 'le', 'len', 'lt', 'ne', 'new', 'or', 'rand', 'reduce', 'reduce_ex', 'repr', 'ror', 'rsub', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', 'xor', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']

    1 长度 len len
    2 包含 in not in contains
    3 for 循环 iter
    4 交集 & iand {}.intersection({})
    5 并集 | ior {}.union({})
    6 差集 - {}.difference({}) 不会覆盖原先set
    7 对称差集 ^ ixor
    8 是否相等 ==
    9 是否子集 >= s1>=s2 issubset
    10 差集合 并且赋值给 {} {}.difference_update({}) 结果会覆盖掉源set
    11 只有左边的set存在的值 symmetric_difference
    12 remove 删除 不存在报错
    13 add 添加
    14 discard 删除,不存在时候不会报错
    15 pop() 删除随机一个值
    16 update() 将新值塞入原先的set 并且变更原先的set
    17 **_update 都会变更原先的set
    18 isdisjoint 有共同部分时候显示false
    19 issuperset 将2个元素合并,并返回第一个集合

    exp

    有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合

    pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
    linuxs={'wupeiqi','oldboy','gangdan'}

    求出即报名python又报名linux课程的学员名字集合

    print(pythons & linuxs)

    求出所有报名的学生名字集合

    print(pythons | linuxs)

    求出只报名python课程的学员名字

    print(pythons - linuxs)

    求出没有同时这两门课程的学员名字集合

    print(pythons ^ linuxs)

    字符编码
    1 py2 默认编码 ascii
    2 py3 默认编码 utf-8

    用什么编码保存,什么编码读取

    unicode --编码encode ---gbk
    gbk --编码decode ---unicode

    字符编码表
    ASCII:只有英文还键盘上所有的符号----》数字
    1英文字符=1Bytes=8bytes

    GBK:中文、英文=====》数字
        1中文字符=》2Bytes=16bit
        1英文字符=》1Bytes=8bit
    
    
    unicode: 万国编码
        2Bytes代表一个字符,无论是英文还是中午
    
    utf-8:
        1英文=1Bytes
        1中字符=3Bytes
    

    都写文件

    with open('access.log','r',encoding='utf-8') as f:

    for line in f:

    print(line,end='')

    with open('access.log','rb') as f:

    for line in f:

    print(line,end='')

    with open('1.png','rb') as f:

    for line in f:

    print(line,end='')

    cp src_file_path dst_file_path

    修改文件的2种方法

    修改文件内容方式一:

    with open('src.txt','r',encoding='utf-8') as f:

    data=f.read()

    # print(type(data),data)

    data=data.replace('武大郎','武藤兰')

    with open('src.txt','w',encoding='utf-8') as f:

    f.write(data)

    修改文件内容方式二:

    import os

    with open('src.txt','r',encoding='utf-8') as f,
    open('src.txt.swap','w',encoding='utf-8') as f_new:
    for line in f:
    if '武佩奇' in line:
    line=line.replace('武佩奇','苍井空')
    f_new.write(line)

    os.remove('src.txt')
    os.rename('src.txt.swap','src.txt')

  • 相关阅读:
    C# 不用添加WebService引用,调用WebService方法
    贪心 & 动态规划
    trie树 讲解 (转载)
    poj 2151 Check the difficulty of problems (检查问题的难度)
    poj 2513 Colored Sticks 彩色棒
    poj1442 Black Box 栈和优先队列
    啦啦啦
    poj 1265 Area(pick定理)
    poj 2418 Hardwood Species (trie树)
    poj 1836 Alignment 排队
  • 原文地址:https://www.cnblogs.com/liuqimin/p/8836317.html
Copyright © 2011-2022 走看看