The filter for Python built-in functions

Source: Internet
Author: User

One: Filter

Help on built-in function filter in module __BUILTIN__: Filter (...) Filter (function or None, sequence), list, tuple, or string Return those items of sequence for which function (i  TEM) is true.  If function is None, and return the items is true. If sequence is a tuple or string, return the same type, else return a list.

Description

Filter (FUNC,SEQ)

Invokes a Boolean function, Func, to iterate through the elements in each SEQ, and returns a sequence of elements that enable Func to return a value of true

   

Example:

>>> list1=[1,2,3]--Define a list >>> filter (NONE,LIST1)--if function is None, then the return sequence is all [1, 2, 3]>>     > >>> list2=[1,2,3,4]>>> def f1 (x):---for each item in sequence x is judged sequentially, if a condition is met, returns True, does not satisfy the return false ... if x>2: ... Return True ... else: return False ... >>> filter (F1,LIST2)-Returns the item that satisfies the condition in the function F1, if the sequence is Ganso, the returned result is also the progenitor, the string and list similarly [3, 4]>>>

The actual case

For example, require output/etc/passwd password file all end-of-/bin/bash users to a list

Programming thinking: You can use the ReadLines function to output the/etc/passwd file to a list, and then write a Boolean function to determine if the list of substrings is the end of the/bin/bash, then return to true, if not then return false, The filter function can be used to achieve the required

Python code is as follows

#!/usr/bin/env python# coding:utf-8#  @Filename:  checkpwd.pt def show_file (file):      ' Open file and returns the file content '          fp = open (file)     contentList =  Fp.readlines ()     fp.close ()     return contentlist  def  filter_line (Listarg):     "Filter out the lines that end  IN /BIN/BASH IN THE /ETC/PASSWD '         if  listarg.strip (' \ n '). EndsWith ('/bin/bash '):        return  true    else:        return false         def show_user (Filterarg):     "Append the  user tO list '         showlist=[]    for i  in filterarg:        showlist.append (I[:i.index (': ')])     print showList    if __name__ ==  ' __main__ ':     file =  '/etc/passwd '     clist = show_file (file )     flist = filter (filter_line,clist)     show_user (flist)

Execution results such as

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/8A/BF/wKiom1g6aY3TWPkvAAAZHU2mIbI493.png-wh_500x0-wm_3 -wmp_4-s_588297671.png "title=" 23acefb9-e283-47d1-a2d8-186c6ba63873.png "alt=" Wkiom1g6ay3twpkvaaazhu2mibi493.png-wh_50 "/>

This article is from the "mirror is not Taiwan" blog, please be sure to keep this source http://kk876435928.blog.51cto.com/3530246/1877014

The filter for Python built-in functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.