Python内置函数(六)—— r/s/t/v/z/import
一、range(start, stop[, step]) —— 返回可迭代对象(类型是对象)
args:
start: 计数从start开始。默认是从0开始;
stop: 计数到stop结束,但不包括stop;
step: 步长,默认是1。
>>> range(10) # 不像python2一样返回一个list range(0, 10) >>> for i in range(6): ... print (i) ... 0 1 2 3 4 5 >>> list(range(6)) # 将range返回的可迭代对象转为一个列表 [0, 1, 2, 3, 4, 5] >>> list(range(0)) [] >>> list(range(0, 66, 6)) [0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60] >>> list(range(0, -10, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] >>> list(range(1, 0)) []
二、repr(object) —— 将对象转化为供解释器读取的形式,返回一个对象的string格式
>>> s = "chaclin" >>> repr(s) "'chaclin'" >>> dict = {"suchdo": "SUCHDO", "chaclin": "Chaclin"} >>> repr(dict) "{'suchdo': 'SUCHDO', 'chaclin': 'Chaclin'}"
三、reversed(seq) —— 返回一个反转的迭代器(seq可以是tuple、string、list、range)
>>> SeqTuplet = (6, "c", 7, "V") >>> print(list(reversed(SeqTuplet))) # 反转元组 ['V', 7, 'c', 6] >>> SeqString = "Chaclin" >>> print(list(reversed(SeqString))) # 反转字符串 ['n', 'i', 'l', 'c', 'a', 'h', 'C'] >>> SeqList = [1, 2 ,"Hello", 6] >>> print(list(reversed(SeqList))) # 反转列表 [6, 'Hello', 2, 1] >>> SeqRange = range(2, 6) >>> print(list(reversed(SeqRange))) # 反转Range [5, 4, 3, 2]
四、round(x [, n]) —— 返回浮点数x的四舍五入值
args:
x: 数字表达式
n: 四舍五入到第n位,其中x需要四舍五入,默认值是0
>>> round(70.23456) 70 >>> round(66.666) 67 >>> round(66.26, 1) 66.3 >>> round(-100.67145, 3) -100.671 >>> round(100.00066, 2) # 有0的都会只保留一位小数 100.0
五、set(iterable) —— 创建一个无序不重复元素集,可进行关系测试,删除重复数据,计算交差集、并集等
>>> x = set("suchdo") >>> x {'o', 'd', 'h', 'u', 's', 'c'} >>> y = set("google") >>> y {'e', 'o', 'l', 'g'} # 重复元素被删除(无序) >>> x & y # 交集 {'o'} >>> x | y # 并集 {'o', 'd', 'h', 'l', 'g', 'u', 'e', 's', 'c'} >>> x - y # 差集 {'d', 'h', 'u', 's', 'c'}
六、setattr() —— 设置属性值,对应getattr()函数,该属性不一定存在,会创建一个新的对象属性,并赋值
args:
object: 对象
name: 字符串,对象属性
value: 属性值
对已存在的属性进行赋值::
>>> class Temp(object): ... bar = 1 ... >>> a = A() >>> temp = Temp() >>> getattr(temp, "bar") 1 >>> setattr(temp, "bar", 6) >>> temp.bar 6
如果属性不存在,会创建新的对象属性,并赋值::
>>> setattr(temp, "what", 21) >>> temp.what 21
七、slice(start, stop[, step]) —— 切片操作,返回一个切片对象
args:
start: 起始位置
stop: 结束位置
step: 间距(步长)
>>> arr_list = list(range(5))[slice(3)] >>> arr_list [0, 1, 2]
八、sorted(iterable, key=None, reverse=False) —— 对所有可迭代对象进行排序操作,返回重新排序的列表
args:
iterable: 可迭代对象
key: 主要是用来进行比较的元素,
reverse: 排序规则,reverse=True 降序,reverse=False 升序(默认)
sort与sorted区别:
sort 是应用在list上的方法,对已存在的列表进行操作;
sorted可以对所有可迭代的对象进行排序操作,会返回新的list,不在原基础上改动
>>> temp = [2, 6, 1, 5] >>> sorted(temp) [1, 2, 5, 6] >>> temp # 原有list并未改动 [2, 6, 1, 5] >>> temp.sort() >>> temp # 原list变化了 [1, 2, 5, 6]
利用key对数字/字典进行排序:
>>> array = [{"age": 20, "name": "fox"}, {"age": 25, "name": "chaclin"}, {"age": 10, "name": "tongzi"}]
>>> sorted(array, key=lambda x:x["age"])
[{'age': 10, 'name': 'tongzi'}, {'age': 20, 'name': 'fox'}, {'age': 25, 'name': 'chaclin'}]
九、staticmethod() —— 装饰器,静态方法声明,可以作为类方法,也可以作为对象方法调用。
十、str(object) —— 将对象转化为字符串
十一、sum(iterable[, start]) 对系列进行求和计算
args:
iterable: 可迭代对象,如:列表、元组、集合
start: 指定相加的参数,如果没有设置这个值,默认为0
>>> a = [1, 2, 3] >>> sum(a) 6 >>> sum(a, 6) # 计算总和后再加6 12
十二、super(type[, object-or-type]) —— 解决多重继承问题,调用父类(超类)的一个方法
https://www.runoob.com/python/python-func-super.html
十三、tuple(seq) —— 将序列转换为元组
noted: 将字典转换为元组时,只保留key!!!!!!
>>> temp = {"a": 1, "b": 2, "c": 3}
>>> tuple(temp)
('a', 'b', 'c')
十四、type(object) —— 返回object的类型
type(name, bases, dict) —— 返回新的type对象 怎么用 ??
args:
name: 类的名称
bases: 基类的元组
dict: 字典,类内定义的命名空间变量
十五、vars([object]) —— 返回对象的属性和属性值的字典对象,如果没有参数,就打印当前调用位置的属性和属性值,类似locals()。
>>> class Chaclin(object): ... c = 1 ... >>> print(vars(Chaclin)) {'__module__': '__main__', 'c': 1, '__dict__': <attribute '__dict__' of 'Chaclin' objects>,
'__weakref__': <attribute '__weakref__' of 'Chaclin' objects>, '__doc__': None}
十六、zip() —— 将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,返回有这些元组组成的对象。
>>> a = [1, 2, 3] >>> b = [3, 4, 5] >>> zip(a, b) <zip object at 0x00000267B889C288> >>> list(zip(a, b)) [(1, 3), (2, 4), (3, 5)]
>>> c = [5, 6, 7] >>> d = [7, 8, 9, 10] >>> list(zip(c, d)) # 元素个数与最短的列表一致 [(5, 7), (6, 8), (7, 9)]
a = [1, 2, 3] b = [4, 5, 6] c = zip(a, b) d, f = zip(*zip(a, b)) # 与zip相反,zip(*)可理解为解压,返回二维矩阵 print(c) print(*zip(a, b)) print(d) print(f) 执行结果: <zip object at 0x000001C676575A08> (1, 4) (2, 5) (3, 6) (1, 2, 3) (4, 5, 6)
十七、__import__() —— 动态加载类和函数
浙公网安备 33010602011771号