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.