python基础补充

python2 和python3的区别

  • 字符编码
    python2中的三种数据类型:unicode, str=bytes,这几种数据类型都是类。
    万国码,世界上所有的文字全都可以包含进来,四个字节表示一个字符,现今还没有被占满,只占到了21位,但是unicode如果存在硬盘上会浪费空间,一个字符占四个字节,进行本地磁盘存储或者进行网络传输时,可以进行优化,于是产生了GBK,utf-8(比GBK容纳的数据多一些),有利于存储和网络传输,将unicode进行压缩后变成了str类型,可以使用GBK或utf-8压缩,本质上是字节,所以在python2中使用str进行网络传输,可以节省流量,网络传输时使用的send内容为str类型。
    python2里面bytes类型= str,python3里面没有unicode类型,只有str,python3中的bytes就是字节,python3中的str就是python2中的unicode,str通过utf-8 转化为bytes,网络传输时:python2传的是‘skdj’, python3传的是b’dkfj’,本质上都是字节, 所以python2中的unicode类型就是python3中的str类,str类是python3中的bytes类。
    图示:

在这里插入图片描述
示例:
python3:

n = '开始'

print(type(n))

a = n.encode('utf8')


print(a, len(a), type(a))


b = a.decode('utf8')


print(b, len(b), type(b))

运行结果

<class 'str'>
b'\xe5\xbc\x80\xe5\xa7\x8b' 6 <class 'bytes'>
开始 2 <class 'str'>

python2

#coding=utf8

n = u'开始'

print(type(n))


a = n.encode('utf8')


print(a, len(a), type(a))



c = a.decode('utf-8')


print(c, len(c), type(c))

运行结果

<type 'unicode'>
('\xe5\xbc\x80\xe5\xa7\x8b', 6, <type 'str'>)
(u'\u5f00\u59cb', 2, <type 'unicode'>)
  • 解释器编码
    python2解释器默认编码ascii,所以只要有中文必须开头要加coding=utf-8
    python3解释器默认编码utf-8
    什么是解释器编码:
    python是一个软件,在执行时,会打开当前的文件,把代码中的字符串全部加载到内存,然后对代码进行编译,语法分析等,每个文字以指定的解释器编码方式转换成010101计算机懂的二进制。(一般不用unicode编码,写在文件中特别浪费空间)
  • range和xrange
  • 新式类和经典类
  • yield from,python3.3之后才有yield from
    yield from作用:把生成器流程转到执行另外一个函数
    python2没有yield from
    对比代码:
    python2:
def func2():
    yield 'a'
    yield 'b'

def func():
    yield 1
    yield from func2()
    yield 2
    yield 3


g = func()

for l in g:
    print(l)

运行结果

  File "C:/test/df.py", line 42
    yield from func2()
             ^
SyntaxError: invalid syntax

python3:

def func():
    yield 1
    yield 2
    yield 3


gen = func()

for line in gen:
    print(line)




def func2():
    yield 'a'
    yield 'b'

def func():
    yield 1
    yield from func2()
    yield 2
    yield 3


g = func()

for l in g:
    print(l)

运行结果

1
2
3
1
a
b
2
3

构造数据结构,内存地址的引用,字典的查找速度比列表快

info = [
    {'id': 1, 'name': 'A', 'pid': None},
    {'id': 2, 'name': 'B', 'pid': None},
    {'id': 3, 'name': 'C', 'pid': 1},
    {'id': 4, 'name': 'D', 'pid': 2},
    {'id': 5, 'name': 'E', 'pid': 3},
    {'id': 6, 'name': 'F', 'pid': 4},
]

info_dict = {}

for l in info:
    l['children'] = []
    info_dict[l['id']] = l

print(info_dict)

result = []
for k, v in info_dict.items():
    pid = v.get('pid')
    if not pid:
        result.append(v)
        continue
    info_dict[pid]['children'].append(v)


print(info_dict)
print(info)
print(result)

运行结果

D:\python3.6\python.exe "D:/学习资料/day01/python基础.py"
{1: {'id': 1, 'name': 'A', 'pid': None, 'children': []}, 2: {'id': 2, 'name': 'B', 'pid': None, 'children': []}, 3: {'id': 3, 'name': 'C', 'pid': 1, 'children': []}, 4: {'id': 4, 'name': 'D', 'pid': 2, 'children': []}, 5: {'id': 5, 'name': 'E', 'pid': 3, 'children': []}, 6: {'id': 6, 'name': 'F', 'pid': 4, 'children': []}}
{1: {'id': 1, 'name': 'A', 'pid': None, 'children': [{'id': 3, 'name': 'C', 'pid': 1, 'children': [{'id': 5, 'name': 'E', 'pid': 3, 'children': []}]}]}, 2: {'id': 2, 'name': 'B', 'pid': None, 'children': [{'id': 4, 'name': 'D', 'pid': 2, 'children': [{'id': 6, 'name': 'F', 'pid': 4, 'children': []}]}]}, 3: {'id': 3, 'name': 'C', 'pid': 1, 'children': [{'id': 5, 'name': 'E', 'pid': 3, 'children': []}]}, 4: {'id': 4, 'name': 'D', 'pid': 2, 'children': [{'id': 6, 'name': 'F', 'pid': 4, 'children': []}]}, 5: {'id': 5, 'name': 'E', 'pid': 3, 'children': []}, 6: {'id': 6, 'name': 'F', 'pid': 4, 'children': []}}
[{'id': 1, 'name': 'A', 'pid': None, 'children': [{'id': 3, 'name': 'C', 'pid': 1, 'children': [{'id': 5, 'name': 'E', 'pid': 3, 'children': []}]}]}, {'id': 2, 'name': 'B', 'pid': None, 'children': [{'id': 4, 'name': 'D', 'pid': 2, 'children': [{'id': 6, 'name': 'F', 'pid': 4, 'children': []}]}]}, {'id': 3, 'name': 'C', 'pid': 1, 'children': [{'id': 5, 'name': 'E', 'pid': 3, 'children': []}]}, {'id': 4, 'name': 'D', 'pid': 2, 'children': [{'id': 6, 'name': 'F', 'pid': 4, 'children': []}]}, {'id': 5, 'name': 'E', 'pid': 3, 'children': []}, {'id': 6, 'name': 'F', 'pid': 4, 'children': []}]
[{'id': 1, 'name': 'A', 'pid': None, 'children': [{'id': 3, 'name': 'C', 'pid': 1, 'children': [{'id': 5, 'name': 'E', 'pid': 3, 'children': []}]}]}, {'id': 2, 'name': 'B', 'pid': None, 'children': [{'id': 4, 'name': 'D', 'pid': 2, 'children': [{'id': 6, 'name': 'F', 'pid': 4, 'children': []}]}]}]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_42233629/article/details/88021208