• python day3


    1.深浅拷贝

    2.字典的扩展
    3.集合
    4.函数
    字符串相当于字符数组
     
    print(sys.getrefcount(c)) 可以获得这个变量的被引用的次数
     
    深复制可使用copy模块中的deepcopy()实现

    深拷贝

    d拷贝了a对象及对象里面的数据,修改a不会影响d,修改a里面的子数据也不会影响d

    浅拷贝

    copy.copy()

    修改对象c,不会影响到对象a,也不会影响到对象c

    修改对象a,不会影响到对象c,但是会影响到对象b

    修改对象b,不会影响到对象c,但是会影响到对象a

    一.set集合
    是一个无序且不重复的可hash的元素集合
    不支持索引,元素获取操作 切片
    必须赋予一个可迭代对象
    应用于爬虫
    1.访问速度快
    2.解决重复的问题
     
     
    <wiz_code_mirror>
     
     
     
     
     
    s1 = set([1,2,3])
    s2 = set([2,3,4])
    s3 = set("xyz")
    s1.update(s3)
    s3.pop()
    print (s1)
    print(s1 & s2) #直接运算调用运算符
    print(s1.intersection(s2))#调用内置应用
    print(s1.symmetric_difference(s2))
    print(s1|s2)
    print(s3)
    输出
    {1, 2, 3, 'z', 'y'}
    {2, 3}
    {2, 3}
    {1, 4}
    {1, 2, 3, 4}
    {'z', 'y'}
     
     
     
    #add 在集合中添加一个元素,如果重复,只会显示一个元素,避免添加字符串时,集合将字符串分割,我们可以用add来添加元素。
    s1 = set()
    s1.add('zzn')
    print(s1)
    s1.add('zzn')
    print(s1)
    输出:
    {'zzn'}
    {'zzn'}
    #clear 清空集合中的元素
    s1.clear()
    print(s1)
    输出:
    set()
    #difference 比对两个集合,把没有重复的元素摘出来,生成一个新的集合
    s2 = set (['zzz','zzz','edsg','ff'])
    print (s2)
    s3 = s2.difference(['zzz','ff'])
    print(s3)
    输出:
    {'edsg'}
    #difference_update 比对两个集合,删除当前集合中所有的包含在参数集合里的元素
    s2 = set (['zzz','zzz','edsg','ff'])
    s4 = s2.difference_update(['zzz','ff'])
    print (s2)
    print (s4)
    输出:
    {'edsg'} None
    #intersection 取交集 创建一个新的集合
    #pop 随机取出一个元素,可以赋值变量
    s2 = set (['zzz','zzz','edsg','ff'])
    ret = s2.pop()
    print (s2)
    输出:
    {'edsg', 'ff'}
    {‘zzz’}
    #remove  移除一个元素,可以指定
    s2.remove('ff')
    print(s2)
    输出:
    {'zzz','edsg'}
    #symmetric_difference  比对两个集合,所有不重复的元素生成一个新的集合
    s1 = set([11,22,33])
    s2 = set([22,44])
    ret1 = s1.difference(s2)
    ret2 = s1.symmetric_difference(s2)
    print (ret1)
    print (ret2)
    输出:
    {33, 11}
    {33, 11, 44}
    二.计数器(counter)
    是的字典的补充,追逐值出现的次数
    1.#counter
    使用需要import collections 把每个元素出现的次数进行统计
    test = collections.Counter('addsfjklsdjflkjslkfj378876')
    print (test)
    输出:
    Counter({'j': 4, 'd': 3, 'l': 3, 's': 3, 'f': 3, 'k': 3, '7': 2, '8': 2, 'a': 1, '3': 1, '6': 1})
    2.#most_common() 拿到前四个最多的值
    import collections test = collections.Counter('addsfjklsdjflkjslkfj378876') print (test) ret = test.most_common(4) print (ret)
    输出:
    Counter({'j': 4, 'f': 3, 'l': 3, 'k': 3, 'd': 3, 's': 3, '7': 2, '8': 2, '3': 1, '6': 1, 'a': 1}) [('j', 4), ('f', 3), ('l', 3), ('k', 3)]
    3.#elements 可把里面的所有的值取出来
    import collections
    test = collections.Counter('addsfjklsdjflkjslkfj378876')
    for n in test.elements():
    print (n)
    6 s s s f f f d d d k k
    。。。。
    4.#uptate 更新
    import collections
    test = collections.Counter([22,23,12])
    test.update(['2','3','444'])
    print (test)
    输出:
    Counter({'3': 1, 22: 1, 23: 1, '2': 1, 12: 1, '444': 1})
    5.#subtract 没有出现过的 -1表示
    import collections
    test = collections.Counter([22,23,12])
    test.subtract(['dd','dee',23])
    print (test)
    输出:
    Counter({12: 1, 22: 1, 23: 0, 'dee': -1, 'dd': -1})
    三.有序字典 orderedDict
    也是字典的补充,记住了元素添加的顺序
    import collections
    dic = collections.OrderedDict()
    dic['ee'] = 'rt'
    dic['ss'] = 'ed'
    dic['sx'] = 'vf'
    print (dic)
    输出:
    OrderedDict([('ee', 'rt'), ('ss', 'ed'), ('sx', 'vf')])
    #move_to_end  把指定的值拿到最后
    import collections
    dic = collections.OrderedDict()
    dic['ee'] = 'rt'
    dic['ss'] = 'ed'
    dic['sx'] = 'vf'
    dic.move_to_end('ee')
    print (dic)
    输出:
    OrderedDict([('ss', 'ed'), ('sx', 'vf'), ('ee', 'rt')])
    #pop 指定拿去对应的值
    dic.pop('ss')
    print (dic)
    输出:
    OrderedDict([('ee', 'rt'), ('sx', 'vf')])
    #popitem 按顺序拿取对应的值从最后开始拿
    dic.popitem()
    print (dic)
    输出:
    OrderedDict([('ee', 'rt'), ('ss', 'ed')])
    #setdefault 设置默认值 默认添加到最后
    dic.setdefault('k4','er')
    print (dic)
    输出:
    OrderedDict([('ee', 'rt'), ('ss', 'ed'), ('sx', 'vf'), ('k4', 'er')])
    #update  把两个字典合成新的字典
    dic.update({'k1':'v1','k2':'v2'})
    print (dic)
    输出:
    OrderedDict([('ee', 'rt'), ('ss', 'ed'), ('sx', 'vf'), ('k2', 'v2'), ('k1', 'v1')])
    四.默认字典defaultdict
    import collections
    dic = collections.defaultdict(list)
    dic['k1'].append('zzn')
    print (dic)
    输出:
    defaultdict(<class 'list'>, {'k1': ['zzn']})
    五.可命名元祖
    根据nametuple可以创建一个包含tuple 所有功能及其他功能的类型
    import collections
    my_clss = collections.namedtuple('clss',['x','e','w'])
    obj = my_clss(11,22,33)
    print (obj.x)
    print (obj.e)
    print (obj.w)
    输出:
    11
    22
    33
    如果括号中只有字符或数字没有逗号,就不是元组,
    (2)这不是元组
    (2,)这是元组
    
    
    六.双向队列 单向队列 
    1.双向队列 deque
    import collections
    d = collections.deque()
    d.append('d')
    d.appendleft('d1')
    d.appendleft('xd')
    print (d)
    r = d.count('d')
    print(r)
    输出:
    deque(['xd', 'd1', 'd1'])
    2.extend 扩展多个元素放进去
    d.extend(['tt','ee','dd'])
    print (d)
    输出:
    d.extend(['tt','ee','dd'])
    print (d)
    #index 取这个元素的位置
    #insert 插入
    #pop 默认从右取
    #rotate 把左边的放到右边
    d.rotate(4)
    print(d)
    输出:
    deque(['xd', 'd1', 'd1', 'tt', 'ee', 'dd'])
    deque(['d1', 'tt', 'ee', 'dd', 'xd', 'd1'])
    单向队列 import queue
    import queue
    q = queue.Queue()
    q.put('123')
    q.put('344')
    print (q.qsize())
    print (q.get())
    输出:
    2
    123

    json 模块

    import json
    s = input('')
    c = json.loads(s)
    print (c)
    print (type(c))
    {"ss":12,"op":45} #必须用双引号
    输出结果:
    {'ss': 12, 'op': 45}
    <class 'dict'>
    [11,22,22]
    <class 'list'>
    
    
    r+ 是可读可写可追加的
    w+  是可写可读的
    a+  是可读写的可追加
    
    
     
    <wiz_code_mirror>
     
     
     
     
     
    with open(Dir+"\test.txt","a+",encoding="utf-8") as f:
        print(f.read())
    with open(Dir+"\test.txt","a+",encoding="utf-8") as f,
            open(Dir+"\test1.txt","a+",encoding="utf-8") as f1:
            print(f.read(),f1.read())
     
     
    
    
    
    
    
    
    作业:
    1、ha.py
    global       
            log 127.0.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    defaults
            log global
            mode http
            timeout connect 5000ms
            timeout client 50000ms
            timeout server 50000ms
            option  dontlognull
    
    listen stats :8888
            stats enable
            stats uri       /admin
            stats auth      admin:1234
    
    frontend oldboy.org
            bind 0.0.0.0:80
            option httplog
            option httpclose
            option  forwardfor
            log global
            acl www hdr_reg(host) -i www.oldboy.org
            use_backend www.oldboy.org if www
    
    backend test.oldboy.org
            server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
    		server 100.1.7.999 100.1.7.999 weight 20 maxconn 3000
    		
    
    backend buy.oldboy.org
            server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
    		
    		
    {"backend": "test.oldboy.org",
     "record":{
    		"server": "100.1.7.999",
    		"weight": 20,
    		"maxconn": 30
      }
    }
    2、写程序
    	用户输入
    		‘{"backend": "test.oldboy.org","record":{"server": "100.1.7.999","weight": 20,"maxconn": 30}}’
    	
    	字符串格式化,、
    		test.oldboy.org
    		server 100.1.7.999 100.1.7.999 weight 20 maxconn 3000
  • 相关阅读:
    UITabar 设置字体大小/颜色
    NSURLSession的基本使用
    报错/警告提示
    实现毛玻璃模糊效果/DRNRealTimeBlur
    免证书真机调试
    xcode添加音效
    NSCalenda日历类
    NSDate--日期格式
    NSArray其中的方法--遍历,
    Mysql学习笔记004
  • 原文地址:https://www.cnblogs.com/nansenblog/p/9337534.html
Copyright © 2020-2023  润新知