|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求教部分见课堂视频截图。
def odd(x):
return x%2
如果让 range(10)的 0,1,2,3,4,5,6,7,8,9 依次代入odd这个函数
那么结果应该是:0,1,0,1,0,1,0,1,0,1
为什么 fliter(odd,range(10))中的odd 就代表0,最终踢掉所有的偶数呢?
为啥odd不能代表1,踢掉所有的奇数呢?
filter(function, iterable)
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
以后遇到问题了,先按F1 看说明文档,一般小的问题,看了以后都能明白
第一句说的就很明白了
Construct an iterator from those elements of iterable for which function returns true.
这个函数会滤掉所有函数值返回为true的值
你这个程序里面,odd函数输入为奇数的时候,返回1,(所有非零数字都判定为true),所以所有的奇数就都过滤掉了
你如果想要得到奇数序列,那就把odd函数改成 return x%2-1就可以了
|
-
|