Python filter function filter () filters a sequence instance using a custom function

Source: Internet
Author: User
Filter function:

The filter () function allows filtering of sequences, meaning that a custom function can be used to filter a sequence, to process each item of the sequence into a custom filter function, and to return the result for filtering. Finally returns the filtered results once.

The filter () function has two parameters:

First, custom function name, required
The second, the column that needs to be filtered, is also necessary

DEMO

requirements, filter more than 5 less than 10 of the number:

The code is as follows:


# Coding=utf8
# defines a function greater than 5 less than 10
def guolvhanshu (num):
If Num>5 and num<10:
Return num

# define a sequence
Seq= (12,50,8,17,65,14,9,6,14,5)

# Use the filter function
Result=filter (GUOLVHANSHU,SEQ)

# (8,9,6)
Print result


Execution Result:

The code is as follows:


(8, 9, 6)


Because 8,9,6 is more than 5, less than 10 is filtered down.
  • Related Article

    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.