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

Flattening sequences

Sometimes, we'll have zipped data that needs to be flattened. For example, our input could be a file that has columnar data. It looks like this:

2     3      5      7     11     13     17     19     23     29
31    37     41     43    47     53     59     61     67     71
...

We can easily use (line.split() for line in file) to create a sequence. Each item within that sequence will be a 10-item tuple from the values on a single line.

This creates data in blocks of 10 values. It looks as follows:

>>> blocked = list(line.split() for line in file)
>>> blocked
[['2', '3', '5', '7', '11', '13', '17', '19', '23', '29'], ['31', '37', '41', '43', '47', '53', '59', '61', '67', '71'], ['179', '181', '191', '193', '197', '199', '211', '223', '227', '229']]

This is a start, but it isn't complete. We want to get the numbers into a single, flat sequence. Each item in the input is a 10 tuple; we'd rather not deal with decomposing this one item at a time.

We can use a two-level generator expression, as shown in the following code snippet, for this kind of flattening:

>>> (x for line in blocked for x in line)
<generator object <genexpr> at 0x101cead70>
>>> list(_)
['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', 
'37', '41', '43', '47', '53', '59', '61', '67', '71',
... ]

The first for clause assigns each item—a list of 10 values— from the blocked list to the line variable. The second for clause assigns each individual string from the line variable to the x variable. The final generator is this sequence of values assigned to the x variable.

We can understand this via a simple rewrite as follows:

def flatten(data: Iterable[Iterable[Any]]) -> Iterable[Any]:
for line in data:
for x in line:
yield x

This transformation shows us how the generator expression works. The first for clause (for line in data) steps through each 10-tuple in the data. The second for clause (for x in line) steps through each item in the first for clause.

This expression flattens a sequence-of-sequence structure into a single sequence. More generally, it flattens any iterable that contains an iterable into a single, flat iterable. It will work for list-of-list as well as list-of-set or any other combination of nested iterables.

主站蜘蛛池模板: 德阳市| 绥化市| 浏阳市| 仁怀市| 榆林市| 白城市| 宿松县| 汨罗市| 淅川县| 井冈山市| 淅川县| 大安市| 麦盖提县| 镇江市| 祥云县| 武隆县| 武陟县| 东港市| 邛崃市| 新乐市| 华蓥市| 昌吉市| 玉林市| 姚安县| 乌兰浩特市| 西盟| 揭阳市| 德化县| 自贡市| 梁平县| 马关县| 兴和县| 翼城县| 碌曲县| 大厂| 惠州市| 军事| 基隆市| 河间市| 海丰县| 虞城县|