Python学习记录(一):常用操作

range函数

range(start, stop[, step]) [start, stop) 区间左闭右开,同切片操作

字典的元素个数

len(dict) 返回字典中的元素(键值对)个数

Python字符串包含

assert 断言

表达式为False时触发异常AssertionError。不满足条件时直接返回错误,而不是等程序奔溃后退出。

字符串大小写

转为小写lower(), 转为大写upper()

Ref. https://www.cnblogs.com/cmnz/p/6956984.html

Python之filter函数

说明:7个一组(0-6)、(7-13), 取出每一组中间的那个值,即3, 10, 17等等

常用的切片操作

  • 反转数组
image = image.permute(1, 2, 0).numpy()[:, :, ::-1]

功能:(C, H, W) --> (H, W, C) 和 RGB2BGR

  • 下采样,即间隔取值
image = image[::4, ::4, :]

Python关键字

Note: 代码中的变量不能与内置的关键字相同,并且不同版本Python的内置关键字有可能是不同的

>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
posted @ 2020-09-05 11:18  达可奈特  阅读(169)  评论(0编辑  收藏  举报