Definition: The
filter function in python is used to filter the sequence, filter(func,lst) contains two parameters, the first parameter is a function, the second parameter is a list, func acts on each element in lst, according to the returned result TRUE Or FALSE to decide the choice of the result.
Example: To
give a list containing several integers, to output a new list, the new list is required to contain only the even numbers in the original list.
Code:
lst = input('请输入一个包含若干整数的列表:')#输入一个列表
lst = eval(lst) #eval()将字符串列表转化为列表的形式
print(list(filter(lambda x: x%2 == 0,lst)))#用lambda定义一个函数,是lst中的所有值除以2的余数为0(输出偶数)