官术网_书友最值得收藏!

Working with Collections

Python offers a number of functions that process whole collections. They can be applied to sequences (lists or tuples), sets, mappings, and iterable results of generator expressions. We'll look at Python's collection-processing features from a functional programming viewpoint.

We'll start out by looking at iterables and some simple functions that work with iterables. We'll look at some design patterns to handle iterables and sequences with recursive functions as well as explicit for loops. We'll look at how we can apply a scalar function to a collection of data with a generator expression.

In this chapter, we'll show you examples of how to use the following functions with collections:

  • any() and all()
  • len(), sum(), and some higher-order statistical processing related to these functions
  • zip() and some related techniques to structure and flatten lists of data
  • reversed()
  • enumerate()

The first four functions can be called reductions: they reduce a collection to a single value. The other three functions, zip(), reversed(), and enumerate(), are mappings; they produce a new collection from an existing collection(s). In the next chapter, we'll look at some more mapping and reduction functions that use an additional function as an argument to customize their processing.

In this chapter, we'll start out by looking at ways to process data, using generator expressions. Then, we'll apply different kinds of collection-level functions to show how they can simplify the syntax of iterative processing. We'll also look at some different ways of restructuring data.

In the next chapter, we'll focus on using higher-order collection functions to do similar kinds of processing.

主站蜘蛛池模板: 巫溪县| 睢宁县| 新丰县| 达日县| 阿拉尔市| 靖江市| 额尔古纳市| 女性| 临沭县| 汽车| 姜堰市| 湖南省| 德钦县| 宁海县| 金堂县| 镇巴县| 丁青县| 秦皇岛市| 合江县| 昂仁县| 通江县| 芒康县| 达日县| 且末县| 平定县| 临洮县| 叶城县| 安宁市| 湖北省| 长岛县| 黄冈市| 丁青县| 凤阳县| 隆尧县| 霍林郭勒市| 本溪市| 阜康市| 长宁县| 德兴市| 徐汇区| 云安县|