zoukankan      html  css  js  c++  java
  • Python学习(四) —— 编码

    一、枚举

      enumerate,for i in enumerate(可迭代对象),返回元组,内容是(序列号,可迭代的每一个元素)

                for i,j in enumerate(可迭代对象,开始序列号),返回序列号,可迭代的每一个元素

    a = 'abc'
    b = {'a':1,'b':2}
    for i in enumerate(a):
        print(i)
    #输出:
    #(0, 'a')
    #(1, 'b')
    #(2, 'c')
    
    for i,j in enumerate(a,1):
        print(i,j)
    #输出:
    #1 a
    #2 b
    #3 c

    二、内存地址:id()

      ==比较的是值,is 比较的是内存地址

      小数据池:int:-5~256

           str:1.不能有空格  

                2.长度不能超过20  

                3.不能有特殊字符

    a = []
    b = []
    print(a == b,a is b)  #True False
    
    a = 'abc'
    b = 'abc'
    print(a == b,a is b)  #True True
    
    a = '-6'
    b = '-6'
    print(a == b,a is b)  #True False

    三、编码

      在python3中,str的表现形式为字符串,bytes的表现形式为b字符串,str的实际编码方式是unicode,bytes的编码方式是utf-8、gbk、gb2312...

      在python3中,unicode:1个字符(无论中文、英文、数字),都用4个字节表示

              utf-8:用最少的字节表示一个字符,英文占1个字节,欧洲文字占2个字节,亚洲文字占3个字节

              gbk:英文占1个字节,中文占2个字节

      在python3中,字符串存在内存中是unicode编码方式,不能直接传输或者存储在硬盘,要转成bytes类型,unicode转bytes:a.endode('编码方式'),bytes转unicode:a.decode('编码方式')

              utf-8和gbk、gb2312等编码方式互不识别,如果要转化,要先解码(decode)成unicode方式,再编码(encode)成对应的方式。

    a = 'abc'
    b = a.encode('utf-8')
    print(b,type(b))  #b'abc' <class 'bytes'>
    
    a = '你好'
    b = a.encode('utf-8')
    print(b,type(b))  #b'xe4xbdxa0xe5xa5xbd' <class 'bytes'>
    c = b.decode()
    print(c,type(c))  #你好 <class 'str'>
    d = c.encode('gbk')
    print(d,type(d))  #b'xc4xe3xbaxc3' <class 'bytes'>

    四、集合(set)

      集合用{}表示,集合是无序、不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希。

      特点:去重,把一个列表变成集合,就自动去重了;

            关系测试,测试两组数据之前的交集、差集、并集等关系;

      1、增:add(),update()(迭代着增加)

    set1 = {1,2,3}
    set1.add('abc')
    print(set1)  #{1,2,3,'abc'}
    
    set1 = {1,2,3}
    set1.update('abc')
    print(set1)  #{1,2,3,'a'.'b'.'c'}

      2、删:remove('元素')、pop():随机删除一个元素,并返回这个元素、clear():清空集合

    set1 = {1,2,3,'a','b','c'}
    set1.remove(1)  #删除一个元素
    print(set1)  #{2,3,'a','b','c'}
    
    set1.pop()  #随机删除一个元素
    print(set1)  #{3,'a','b','c'}
    
    set1.clear()  #清空集合
    print(set1)  #set()

      3、集合的其它操作

        3.1、交集(& 或者 intersection)

    set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 & set2)  # {4, 5}
    print(set1.intersection(set2))  # {4, 5}

        3.2、并集(| 或者 union)

    set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 | set2)  # {1, 2, 3, 4, 5, 6, 7,8}
    print(set2.union(set1))  # {1, 2, 3, 4, 5, 6, 7,8}

     

        3.3、差集(- 或者 difference)

    set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 - set2)  # {1, 2, 3}
    print(set1.difference(set2))  # {1, 2, 3}
    print(set2 - set1)  #{8, 6, 7}
    print(set2.difference(set1))  #{8, 6, 7}

        3.4、反交集(^ 或者 symmetric_difference)

    set1 = {1,2,3,4,5}
    set2 = {4,5,6,7,8}
    print(set1 ^ set2)  # {1, 2, 3, 6, 7, 8}
    print(set1.symmetric_difference(set2))  # {1, 2, 3, 6, 7, 8}

        3.5、子集: < 或者 issubset  超集: > 或者 issuperset

    set1 = {1,2,3}
    set2 = {1,2,3,4,5,6}
    
    print(set1 < set2)  # True
    print(set1.issubset(set2))  # True
    
    print(set2 > set1)  # True
    print(set2.issuperset(set1))  # True

      5、frozenset:不可变集合,让集合变成不可变类型

    s = frozenset('barry')
    print(s,type(s))  # frozenset({'a', 'y', 'b', 'r'}) <class 'frozenset'>

     

    五、深浅copy

      1、赋值运算

    l1 = [1,2,3,['a','b']]
    l2 = l1
    
    l1[0] = 111
    print(l1)  # [11, 2, 3, ['a','b']]
    print(l2)  # [11, 2, 3, ['a','b']]
    
    l1[3].append('c')
    print(l1)  # [11, 2, 3, ['a','b','c']]
    print(l2)  # [11, 2, 3, ['a','b','c']]

      对于赋值,指向的是同一个列表,所以更改l1的时候,l2也随之改变

      2、浅拷贝:copy()

    l1 = [1,2,3,['a','b']]
    l2 = l1.copy()
    
    l1[0] = 111
    print(l1)  # [111, 2, 3, ['a','b']]
    print(l2)  # [1, 2, 3, ['a','b']],1没有改变
    
    l1[3].append('c')
    print(l1)  # [111, 2, 3, ['a','b','c']]
    print(l2)  # [1, 2, 3, ['a','b','c']],列表里面的列表变了

      对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存地址,所以,对于第二层以及更深的层数来说,保持一致性。

      3、深拷贝:copy.deepcopy(),pycharm需要import copy

    import copy
    l1 = [1,2,3,['a','b']]
    l2 = copy.deepcopy(l1)
    
    l1[0] = 111
    print(l1)  # [111, 2, 3, ['a','b']]
    print(l2)  # [1, 2, 3, ['a','b']],1没有改变
    
    l1[3].append('c')
    print(l1)  # [111, 2, 3, ['a','b','c']]
    print(l2)  # [1, 2, 3, ['a','b']],列表里面的列表也没有发生改变                                 

      对于深copy来说,两个是完全独立的,改变任意一个的任何元素(无论多少层),另一个绝对不改变

  • 相关阅读:
    basic-linux
    巧用border属性
    git常用操作笔记
    如何删除github里的项目
    常用css3属性的ie兼容查看
    新建pc端页面的模板
    HTML5 Shiv--解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
    进程和线程
    C++对象模型---第 4 章 Function语意学
    C++对象模型---第 3 章 Data语意学
  • 原文地址:https://www.cnblogs.com/Coufusion/p/7750676.html
Copyright © 2011-2022 走看看