最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python中有关filter的用法详解

来源:动视网 责编:小采 时间:2020-11-27 14:22:59
文档

Python中有关filter的用法详解

Python中有关filter的用法详解:Python中有关filter的用法详解1 class filter(object) 2 | filter(function or None, iterable) --> filter object 3 | 4 | Return an iterator yielding those items of iterable for which fun
推荐度:
导读Python中有关filter的用法详解:Python中有关filter的用法详解1 class filter(object) 2 | filter(function or None, iterable) --> filter object 3 | 4 | Return an iterator yielding those items of iterable for which fun


Python中有关filter的用法详解

1 class filter(object)
2 | filter(function or None, iterable) --> filter object
3 | 
4 | Return an iterator yielding those items of iterable for which function(item)
5 | is true. If function is None, return the items that are true.

filter读入iterable所有的项,判断这些项对function是否为真,返回一个包含所有为真的项的迭代器。如果function是None,返回非空的项。

1 In [2]: import re
2 In [3]: i = re.split(',',"123,,123213,,,123213,")
3 In [4]: i4 Out[4]: ['123', '', '123213', '', '', '123213', '']

这时,列表i内包含空串。

1 In [7]: print(*filter(None, i))
2 123 123213 123213

这时filter把列表中的空串过滤掉了,得到一个只含非空串的迭代器。

In [9]: print(list(filter(lambda x:x=='', i)))
['', '', '', '']

由于lambda对空串为真,所以filter把非空串过滤掉,只剩下空串。

文档

Python中有关filter的用法详解

Python中有关filter的用法详解:Python中有关filter的用法详解1 class filter(object) 2 | filter(function or None, iterable) --> filter object 3 | 4 | Return an iterator yielding those items of iterable for which fun
推荐度:
标签: 使用 用法 详解
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top