Python Tiled Sequence

Python Flattening Sequences: Sometimes compressed data needs to be flattened into a one-dimensional sequence. For example, the input file structure may be as follows:

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

You can use (line.split() for line in file) generates a sequence where each element of the sequence is a ten-tuple consisting of a line of data in the file.

The resulting data structure is 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']]

However, this isn’t the desired end result. We expect the structure to be a one-dimensional sequence, but each element in the input is a 10-tuple. Decomposing each tuple individually is too cumbersome.

To flatten this data structure, use a two-level generator expression like the following:

>>> (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 parses objects from the blocked list. Each object is a list of length 10 and is assigned to the line variable. The second for clause parses strings from the line variable and assigns them to the x variable. The generator containing the final result is stored in the x variable.

Rewriting this to the following form is easier to understand:

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

This rewrite shows how the generator expression works: the outer for clause (for line in data) iterates over each decimal in the input data, and the inner for clause (for x in line) iterates over each element in the outer clause.

This converts the double-level sequence composite structure into a single-level sequence. It can convert any nested double-level iterable object into a single-level iterable object, such as a double-level list or a list-set combination structure.

Leave a Reply

Your email address will not be published. Required fields are marked *