- Functional Python Programming
- Steven F. Lott
- 407字
- 2021-08-27 19:20:28
Using zip() to structure and flatten sequences
The zip() function interleaves values from several iterators or sequences. It will create n tuples from the values in each of the n input iterables or sequences. We used it in the previous section to interleave data points from two sets of samples, creating two-tuples.
The following is an example of code that shows what the zip() function does:
>>> xi= [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65,
... 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83,] >>> yi= [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29,
... 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46,] >>> zip( xi, yi ) <zip object at 0x101d62ab8> >>> list(zip( xi, yi )) [(1.47, 52.21), (1.5, 53.12), (1.52, 54.48),
(1.55, 55.84), (1.57, 57.2), (1.6, 58.57),
(1.63, 59.93), (1.65, 61.29), (1.68, 63.11),
(1.7, 64.47), (1.73, 66.28), (1.75, 68.1),
(1.78, 69.92), (1.8, 72.19), (1.83, 74.46)]
There are a number of edge cases for the zip() function. We must ask the following questions about its behavior:
- What happens where then are no arguments at all?
- What happens where there's only one argument?
- What happens when the sequences are different lengths?
As with other functions, such as any(), all(), len(), and sum(), we want an identity value as a result when applying the reduction to an empty sequence. For example, sum(()) should be zero. This concept tells us what the identity value for zip() should be.
Clearly, each of these edge cases must produce some kind of iterable output. Here are some examples of code that clarify the behaviors. First, the empty argument list:
>>> zip() <zip object at 0x101d62ab8> >>> list(_) []
We can see that the zip() function with no arguments is a generator function, but there won't be any items. This fits the requirement that the output is iterable.
Next, we'll try a single iterable:
>>> zip( (1,2,3) ) <zip object at 0x101d62ab8> >>> list(_) [(1,), (2,), (3,)]
In this case, the zip() function emitted one tuple from each input value. This too makes considerable sense.
Finally, we'll look at the different-length list approach used by the zip() function:
>>> list(zip((1, 2, 3), ('a', 'b'))) [(1, 'a'), (2, 'b')]
This result is debatable. Why truncate? Why not pad the shorter list with None values? This alternate definition of the zip() function is available in the itertools module as the zip_longest() function. We'll look at this in Chapter 8, The Itertools Module.
- 手機(jī)安全和可信應(yīng)用開發(fā)指南:TrustZone與OP-TEE技術(shù)詳解
- 計(jì)算機(jī)網(wǎng)絡(luò)
- Learning Scala Programming
- Flask Blueprints
- 自制編譯器
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊(cè))
- Mastering LibGDX Game Development
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- INSTANT Django 1.5 Application Development Starter
- Java編程技術(shù)與項(xiàng)目實(shí)戰(zhàn)(第2版)
- HTML5 APP開發(fā)從入門到精通(微課精編版)
- C語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn)教程與水平考試指導(dǎo)
- Mastering Elixir
- IoT Projects with Bluetooth Low Energy