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

Using enumerate() to include a sequence number

Python offers the enumerate() function to apply index information to values in a sequence or iterable. It performs a specialized kind of wrap that can be used as part of an unwrap(process(wrap(data))) design pattern.

It looks like the following code snippet:

>>> xi
[1.47, 1.5, 1.52, 1.55, 1.57, 1.6, 1.63, 1.65, 1.68, 1.7, 1.73, 
1.75, 1.78, 1.8, 1.83] >>> list(enumerate(xi)) [(0, 1.47), (1, 1.5), (2, 1.52), (3, 1.55), (4, 1.57),
(5, 1.6), (6, 1.63), (7, 1.65), (8, 1.68), (9, 1.7),
(10, 1.73), (11, 1.75), (12, 1.78), (13, 1.8), (14, 1.83)]

The enumerate() function transformed each input item into a pair with a sequence number and the original item. It's vaguely similar to the following:

zip(range(len(source)), source)

An important feature of enumerate() is that the result is an iterable and it works with any iterable input.

When looking at statistical processing, for example, the enumerate() function comes in handy to transform a single sequence of values into a more proper time series by prefixing each sample with a number.

主站蜘蛛池模板: 甘孜| 丰都县| 楚雄市| 安庆市| 江北区| 偏关县| 叶城县| 霍城县| 崇文区| 博客| 朔州市| 科尔| 内江市| 昌邑市| 海晏县| 来安县| 潼关县| 封开县| 安西县| 临高县| 芦溪县| 新竹县| 个旧市| 博野县| 安阳市| 大同县| 永济市| 喜德县| 海安县| 南安市| 莲花县| 林甸县| 定州市| 屏南县| 翁源县| 阿克| 亚东县| 兴隆县| 黔西县| 松江区| 大悟县|