zoukankan      html  css  js  c++  java
  • Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里

    reduce函数:
    在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里 用的话要 先引入:
    >>> from functools import reduce 
    >>> print(l1)
    [0, 1, 2, 3, 4, 5, 6]
    >>> reduce( f4, l1 )
    21

    from functools import reduce
    
    def add(x,y):
        return x + y
    
    print (reduce(add, range(1, 101)))

    输出结果为:

    5050
  • 相关阅读:
    LeetCode 55
    LeetCode 337
    LeetCode 287
    LeetCode 274
    LeetCode 278
    LeetCode 264
    LeetCode 189
    LeetCode 206
    LeetCode 142
    LeetCode 88
  • 原文地址:https://www.cnblogs.com/timssd/p/4765362.html
Copyright © 2011-2022 走看看