函数内置方法2

bytearray()返回一个新字节数组。

s = "Auffei"
s = s.encode()
print(s)   #输出为b"luffei",b前缀代表的是字节。

s = bytearray(s)  #返回一个新字节数组
print(s)
print(id(s))  #输出为 34697320
s[0] = 66
print(s)
print(id(s))  #输出地址同上,两个地址一样。

map()
print(list(map(lambda x:x*x,[1,2,3,4,5])))

filter()快速过滤  lambda函数返回True的结果保留,返回False的结果去掉。

print(list(filter(lambda x:x>3,[1,2,3,4,5,6,7])))  #输出为[4,5,6,7]

reduce() 

import functools
functools.reduce(lambda x,y:x*y,[1,2,3,4,5,6],3)
functools.reduce(lambda x,y:x+y,[1,2,3,4,5,6],3)    #最后的3是位置,上面的是从第三位加到最后,下面的是从第三位乘到最后。


pow(a,b) a的b次方
print(pow(3,3))

默认换行,加入sep 可在空行之间插入。

print("lll","uuu",sep ="<-")

tuple()

callable()可以判断变量是否是函数,函数可以调用,返回True。

format()

frozenset()  不可变的集合

vars()  会同时打印变量名 和变量名对应的值。  dir() 只会打印变量名。

locals()打印当前作用域的局部变量

globals() 打印全局变量

repr() 显示成字符串。

zip()

a = [1,2,3,4,5,6,]
b = ['a','b','n','m','hy','y','tt']
print(list(zip(a,b)))  #[(1, 'a'), (2, 'b'), (3, 'n'), (4, 'm'), (5, 'hy'), (6, 'y')]

complex(a,b) —》 a+bj  把两个数变成复数

round() 取后面小数部分位数。

print(round(1.2345,3))
print(round(1.23456))  #1.234    1

set()  把字符串,列表等变成集合,集合内的值不可重复。

  

  

  

  

  

  

原文地址:https://www.cnblogs.com/Roc-Atlantis/p/8541709.html