- Functional Python Programming
- Steven F. Lott
- 277字
- 2021-08-27 19:20:26
Using stateful sets
Python offers several stateful collections, including the set collection. For our purposes, there are two use cases for a set: a stateful set that accumulates items, and frozenset that is used to optimize searches for an item.
We can create frozenset from an iterable in the same way we create a tuple object from an iterable fronzenset(some_iterable) method; this will create a structure that has the advantage of a very fast in operator. This can be used in an application that gathers data, creates a set, and then uses that frozenset to process some other data items.
We may have a set of colors that we will use as a kind of chroma-key: we will use this color to create a mask that will be used to combine two images. Pragmatically, a single color isn't appropriate, but a small set of very similar colors works best. In this case, we'll examine each pixel of an image file to see if the pixel is in the chroma-key set or not. For this kind of processing, the chroma-key colors are loaded into frozenset before processing the target images. For more information, read about chroma-key processing from the following link:
http://en.wikipedia.org/wiki/Chroma_key
As with mappings—specifically the Counter class—there are some algorithms that can benefit from a memoized set of values. Some functions benefit from memoization because a function is a mapping between domain values and range values, a job where mapping works well. A few algorithms benefit from a memoized set, which is stateful and grows as data is processed.
We'll return to memoization in Chapter 16, Optimizations and Improvements.
- .NET之美:.NET關鍵技術深入解析
- Java多線程編程實戰(zhàn)指南:設計模式篇(第2版)
- Oracle WebLogic Server 12c:First Look
- Kali Linux Web Penetration Testing Cookbook
- OpenCV 3和Qt5計算機視覺應用開發(fā)
- 鋒利的SQL(第2版)
- Spring Boot企業(yè)級項目開發(fā)實戰(zhàn)
- JavaScript+jQuery網頁特效設計任務驅動教程
- R Data Science Essentials
- GitHub入門與實踐
- WCF技術剖析(卷1)
- Android初級應用開發(fā)
- Learning RSLogix 5000 Programming
- jQuery EasyUI從零開始學
- Instant MongoDB