How to use filter, map, and reduce in Python 3

前端 未结 7 1826
夕颜
夕颜 2020-11-22 08:27

filter, map, and reduce work perfectly in Python 2. Here is an example:

>>> def f(x):
        return x % 2 !=         


        
7条回答
  •  猫巷女王i
    2020-11-22 09:00

    Since the reduce method has been removed from the built in function from Python3, don't forget to import the functools in your code. Please look at the code snippet below.

    import functools
    my_list = [10,15,20,25,35]
    sum_numbers = functools.reduce(lambda x ,y : x+y , my_list)
    print(sum_numbers)
    

提交回复
热议问题