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

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.

主站蜘蛛池模板: 湄潭县| 邳州市| 甘洛县| 特克斯县| 白银市| 奉节县| 安泽县| 凤翔县| 大同县| 诸暨市| 故城县| 宁乡县| 南涧| 荔波县| 泰宁县| 呼图壁县| 年辖:市辖区| 巴里| 油尖旺区| 顺平县| 郓城县| 赫章县| 同德县| 疏勒县| 房产| 灌南县| 南昌县| 樟树市| 禹城市| 沙洋县| 凤阳县| 长宁区| 白银市| 昭觉县| 高要市| 黑龙江省| 通道| 水城县| 台安县| 疏勒县| 宾阳县|