- Functional Python Programming
- Steven F. Lott
- 163字
- 2021-08-27 19:20:28
Using len() and sum()
The len() and sum() functions provide two simple reductions—a count of the elements and the sum of the elements in a sequence. These two functions are mathematically similar, but their Python implementation is quite different.
Mathematically, we can observe this cool parallelism. The len() function returns the sum of ones for each value in a collection, .
The sum() function returns the sum of x for each value in a collection, .
The sum() function works for any iterable. The len() function doesn't apply to iterables; it only applies to sequences. This little asymmetry in the implementation of these functions is a little awkward around the edges of statistical algorithms.
For empty sequences, both of these functions return a proper additive identity element of zero:
>>> sum(()) 0
Of course, sum(()) returns an integer zero. When other numeric types are used, the integer zero will be coerced to the proper type for the available data.
- Unity 2020 By Example
- 圖解Java數據結構與算法(微課視頻版)
- Mastering Concurrency in Go
- Learning Elixir
- Hadoop+Spark大數據分析實戰
- Building Minecraft Server Modifications
- Hands-On Functional Programming with TypeScript
- MINECRAFT編程:使用Python語言玩轉我的世界
- Image Processing with ImageJ
- Unity 2018 Augmented Reality Projects
- 算法設計與分析:基于C++編程語言的描述
- 嵌入式C編程實戰
- JavaEE架構與程序設計
- HTML5 WebSocket權威指南
- ArcPy and ArcGIS(Second Edition)