完整版)python常用函数
Practical Standard Document
1.map() n
The map() n is a built-in higher-order n in Python。It takes a n f and a list。and applies the n f to each element of the list in turn to obtain a new list and return it.forbidden用法
For example。if we want to square each element of the list [1.2.3.4.5.6.7.8.9]。we can use the map() n。Therefore。we only need to pass in the n f(x)=x*x XXX using the map() n:
def f(x):
容器类型1类return x*x
print(map(f。[1.2.3.4.5.6.7.8.9]))
Output:
1.4.9.16.25.36.49.64.81]
XXX in a list can be of any type。map() can handle lists that contain any type of data。as long as the passed-in n f can handle this data type.
我的世界execute指令全解Suppose that the English names entered by users are not standardized and do not follow the rule of capitalizing the XXX。We can use the map() n to convert a list (containing several non-standard English names) into a list of standardized English names:特效素材多多
Input: ['adam'。'LISA'。'barT']
Output: ['Adam'。'XXX'。'Bart']
2.ce() n
The ce() n is also a built-in higher-order n in Python。The parameters received by ce() are similar to those of map()。a n f and a list。but the r is different from that of map()。The n f passed to ce() must accept two parameters。The ce() n repeatedly calls the n f for each element in the list and returns the final result value.
For example。we can write an f n that accepts x and y and returns the sum of x and y:
python基础代码大全下载def f(x。y):
return x + y
ce函数时,会对list中的元素进行逐个计算,每次计算都会把前面计算得到的结果和当前元素一起传入函数中进行计算。例如,ce(f。[1.3.5.7.9])会先计算f(1.3),结果为4,然后再计算f(4.5),结果为9,以此类推,直到计算结束。最终的结果就是所有元素的计算结果。ce函数来实现求和等操作。
除了默认的计算方式外,ce函数还可以接收第三个参数,作为计算的初始值。如果设置了初始值,那么第一次计算时会使用初始值和第一个元素进行计算。例如,ce(f。[1.3.5.7.9]。100)会先计算f(100.1),结果为101,然后再计算f(101.3),以此类推,直到计算结束。最终的结果就是所有元素的计算结果加上初始值。
软件测试教程自学网
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论