鱼C论坛

 找回密码
 立即注册
查看: 3303|回复: 1

[萌新报道] 内置函数filter()

[复制链接]
发表于 2018-4-6 00:16:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 您的地址 于 2018-4-8 18:11 编辑

[内置函数] 内置函数:filter [复制链接]
        filter内置函数:
                1.使用:1.可以自己定义一个想要获得的数据函数,然后在其他地方可以使用filter()来筛选你想要的数据.
                2.filter(None,iterable):iterable中的值如果有''',则直接在显示时直接忽略,如果有 '' '' 空置则打印空值


方法:
|  filter(function or None, iterable) --> filter object
|  
|  Return an iterator yielding those items of iterable for which function(item)
|  is true. If function is None, return the items that are true.


自画像:
filter(function, iterable) <-> (item for item in iterable if function(item))
filter(None, iterable) <-> (item for item in iterable if item)


代码:#Python3.4.0
def is_odd(n):
    return n % 2 == 1

list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))
# 结果: [1, 5, 9, 15]

-
>>> list(filter(None, ['A', '', 'B', None, 'C', '  ']))
['A', 'B', 'C', '  ']
>>> #'  '非False

来源:~风介~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-6 10:18:34 | 显示全部楼层
111
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-14 17:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表