python自学笔记6之库函数
2016-09-20 22:15
501 查看
Python 3.5
reduce()函数是需要导入的:
那么问题来了?
从什么地方知道某个想用的函数在哪个库中躲藏着呢?
先来看看有哪些内置函数
可以通过dir()和help()来查看内置函数
dir()可以看看模块里有什么函数,变量,嵌套的模块
help()可以看看函数的帮助文档
用过matlab的应该就知道了
那,这里没有的话,再去哪找?
https://docs.python.org/3/library/functools.html?highlight=reduce#functools.reduce
查找官方文档,search一下,你就知道
我搜索了reduce,结果如下
functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.
Roughly equivalent to:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value
可以确定两点
1. functools.reduce,可以看到reduce 是functools里面的,所以 from functools import reduce,是可以用的
2. 说明文档中,你可以看到reduce函数的具体了,这样自己也能定义一个用,二手打算。
写了个回数判断函数:
如果用到函数的话,一行代码就解决了!
当然,也有不用函数的高级版本
确定算法的条件下,实现方式的不一样,代码执行效率不一样!
reduce()函数是需要导入的:
from functools import reduce
那么问题来了?
从什么地方知道某个想用的函数在哪个库中躲藏着呢?
先来看看有哪些内置函数
可以通过dir()和help()来查看内置函数
dir()可以看看模块里有什么函数,变量,嵌套的模块
help()可以看看函数的帮助文档
用过matlab的应该就知道了
那,这里没有的话,再去哪找?
https://docs.python.org/3/library/functools.html?highlight=reduce#functools.reduce
查找官方文档,search一下,你就知道
我搜索了reduce,结果如下
functools.reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.
Roughly equivalent to:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value
可以确定两点
1. functools.reduce,可以看到reduce 是functools里面的,所以 from functools import reduce,是可以用的
2. 说明文档中,你可以看到reduce函数的具体了,这样自己也能定义一个用,二手打算。
写了个回数判断函数:
def zis_palindrome(s1): s=list(str(s1)) for i in range(len(s)): st=s[i] s[i]=s[len(s)-i-1] s[len(s)-i-1]=st return list(str(s1))==s
如果用到函数的话,一行代码就解决了!
def zis_palindrome(s1): return list(str(s))==list(reversed(str(s)))
当然,也有不用函数的高级版本
def is_palindrome(n): return n == int(str(n)[::-1])
确定算法的条件下,实现方式的不一样,代码执行效率不一样!
相关文章推荐
- Python自学笔记之函数2——定义函数以及函数的参数
- 【笔记】Python 3自学笔记之函数
- Python自学笔记之函数3——递归函数
- Python自学笔记之函数1——调用函数
- Python2.7自学笔记4――定义函数
- python自学笔记之7编写函数、print、版本差距
- python自学笔记(5)--python函数基础
- python自学笔记(6)--Python函数式编程、函数的装饰器、递归
- Python自学笔记之函数式编程5——返回函数
- Python自学笔记(四)函数
- 简明python教程自学笔记(1)
- Python学习笔记整理(十二)Python的函数
- C++猿的Python笔记03-函数
- [笔记]Python虚拟机对函数的解释
- Python学习笔记总结(二):函数和模块
- Python自学笔记(一)(Python基础知识)
- python简明教程学习笔记2--函数
- Python 笔记 : 函数
- Python笔记——数字及数学函数
- Python笔记3:模块与函数