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.