17、dict()
描述:创建数据字典
语法:
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
参数:
**kwargs -- 关键字
mapping -- 元素容器。
iterable -- 可迭代对象。
案例:
#创建空字典
dict(){} #dict输入关键字(a='a',b='b',t='t'){'a':'a','b':'b','t':'t'} #构建字典dict的映射函数(zip(['one','two','three'],[1,2,3]){');three':3,'two':2,'one':1} #构建字典dict的可迭代对象方法('one',1),('two',2),('three',){'three':3,'two':2,'one':
18、dir()
描述:dir() 当函数没有参数时,返回当前范围内的变量、方法和定义类型列表;当带有参数时,返回参数的属性和方法列表。如果参数包含方法__dir__()这种方法将被调用。如果参数不包括__dir__(),该方法将限制参数信息的收集。
语法:dir([object])
参数:object -- 对象、变量、类型。
案例:
dir() #获取当前模块的属性列表 ['In','Out','_','__','___','__builtin__','__builtins__', '__doc__','__loader__','__name__','__package__','__spec__','_dh', '_i','_i1','_ih','_ii','_iii','_oh','exit','get_ipython','quit']
dir([])#dir查看列表的方法(list()) #查看列表['__add__','__class__','__contains__', '__delattr__','__delitem__','__dir__','__str__', '__subclasshook__','append','clear','copy','count', 'extend','index','insert','pop','remove','reverse', 'sort']print(dir(str))#print获取所有方法(dir(list)) #print获取所有方法(dir(dict)) #回到当前范围内的变量,获取所有无参数,方法和定义类型列表;带参数时返回参数的属性,方法列表。
19、pmod()
描述:pmod() 函数将除数和余数运算结果结合起来,返回包含商和余数的元组(a // b, a % b)。
语法:pmod(a, b)
参数:
a: 数字--被除数
b: 数字--除数
案例:
pmod(11,3)(3,2)pmod(20,4)(5,0)
20、enumerate()
描述:enumerate() 函数用于将可遍历的数据对象(如列表、元组或字符串)组合成索引序列,
同时列出数据和数据下标,一般用于 for 循环中。返回一个可以枚举的对象,该对象的next()方法将返回一个元组。
语法:enumerate(sequence, [start=0])
参数:
sequence -- 一个序列,迭代器或其他支持迭代的对象。
start -- 下标起始位置。
案例:
L=['Spring','Summer','Fall','Winter']
enumerate(L)<enumerateateat0x26e1131> #生成的额迭代器不能直接查看list(enumerate(L)) #列表形式可以看到内部结构,默认从0开始下标[(0,'Spring'),(1,'Summer'),(2,'Fall'), (3,'Winter')]list(enumerate(L,start=1)) #下标从1开始[(1)'Spring'),(2,'Summer'),(3,'Fall'),(4,'Winter')]fori,vinenumerate(L): print(i,v)0Spring1Summer2Fall3Winterfori,vinenumerate(L,1): print(i,v)Spring2Sumer3Fal4Winters=["a","b","c"]fori,vinenumerate(s,2): print(i,v)2a3b4c普通for循环i=0seq=['one','two','three']forelementinseq: print(i,seq[i])i+=10one1two2three正在观看一个普通循环的比较案例 for循环使用enumerateseq['one','two','three']fori,elementinenumerate(seq): print(i,element)0one1two2threseq['one','two','three']fori,elementinenumerate(seq,2): print(i,element)2one3two
21、eval()
描述:字符串str 将字符串中的内容作为有效的表达式进行求值,并返回计算结果
语法:eval(expression[, globals[, locals]])
参数:
expression -- 表达式。
globals -- 变量作用域,整体命名空间,如果提供,必须是字典对象。
locals -- 变量作用域,局部命名空间,如果提供,可以是任何映射对象。
案例:
s="1+3+5"eval(s)9 #统计图片的数量str1="['https://ww1.sin5n.jpg','https://ww1.siqk4he.jpg']"len(eval(str1)2len(str1)5
22、exec()
描述:与存储在字符串或文件中的Python语句相比,执行存储在字符串或文件中的Python语句 eval,exec可以更复杂地执行 Python 代码。
语法:exec(object, globals, locals)
参数:
object-- 要执行的表达式。
globals -- 可选的。包含全局参数的字典。
locals -- 可选。字典包含局部参数。
案例:
用compile方法编译的执行字符串或字符串没有返回值s="print('helloworld')" r=compile(s,"<string>","exec")exec(r) helloworldx=10 expr=""" z=30 sum=x+y+zprint(sum)""" deffunc(): y=20 exec(expr) exec(expr,{'x':1,'y':2}) exec(expr,{'x':1,'y':2},{'y':3,'z':4}) func()603334
23、filter()
描述:filter() 函数用于过滤序列,过滤掉不合格元素,返回由合格元素组成的新列表。
接收两个参数,第一个是函数,第二个是序列。序列的每个元素作为参数传递给函数进行判断,然后返回 True 或 False,最后将返回 True 将元素放入新列表中。
过滤器,构建一个序列,等于:[ item for item in iterables if function(item)]
在函数中设置过滤条件,循环迭代器中的元素逐一留下返回值作为True时的元素,形成filter类型数据。
语法:filter(function, iterable)
参数:
function -- 判断函数。
iterable -- 可迭代对象。
案例:
fil=filter(lambdax:x>[1、11、2、45、7、6、13]
fil<filterat0x28b693b28>list(fil)[11,45,13]defis_odd(n): returnn%2==1 newlist=filter(is_odd,[1、2、3、4、5、6、7、8、9、10]) print(list(newlist))[1,3,5,7,9]
24、float()
描述:将字符串或整数转换为浮点数
语法:class float([x])
参数:x -- 整数或字符串
案例:
float(3)3.0float('123')#字符串123.0
详解Python中56个内置函数(1)
Python中56个内置函数的详解(2)
Python中56个内置函数详解(4)
Python中56个内置函数详解(5)
Python中56个内置函数详解(6)
Python中56个内置函数详解(7)