Python filter function filter () filter sequence instances using a custom function _python

Source: Internet
Author: User

Filter function:

The filter () function filters the sequence by using a custom function that filters a sequence, passes each item of the sequence to a custom filter function, and returns the result to be filtered. Finally, the filtered results are returned at 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, filtering more than 5 less than 10:

Copy Code code 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)

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

# (8,9,6)
Print result

Execution results:
Copy Code code as follows:

(8, 9, 6)

Because the 8,9,6 is greater than 5 and less than 10, it 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.