Information nesting 2
Last updated
Last updated
This section demonstrates how to perform basic operations to nested containers, namely merging, flattening, transposing and indexing.
Flattening converts data containers, of any depth of nesting, into regular 1D lists. The operation is accessible via the context menu of both input and output parameters.
The geometric interpretation of flattening, using the “polyline” component, reveals how the points are chained together into scan-lines after columns are merged.
List partitioning has the opposite effect compared to flattening. Starting with a list of “n” numbers we can produce a table by slicing the list every “m” items. Each group of “m” items becomes its own column. The number of items per column is “n / m”.
In the example below we construct 3 x 4 = 12 numbers i.e. the range [0, 11]. Using the “partition list” component we can chop the list into columns of either 3 or 4 rows.
Transpose exchanges rows and columns of a table. Here it is named as the “flip matrix” component. In the example below we have a 3-columns by 2-rows table which we transposed to a 2-column by 3-rows table.
Transpose has the effect of chaining rows instead of columns.
There are many ways to join values and lists. The “merge” component fuses single values into lists eg. see “X” and “Y” and “Z” becomes [“X”, “Y”, “Z”]. Merge has the effect of fusing all their elements into a new list e.g. [“X”, “Y”, “Z”] and [4, 5] becomes [“X”, “Y”, “Z”, 4, 5]. To retain container shapes we may use the “entwine” component which you may consider as a “stacking lists into tables” operation e.g. [“X”, “Y”, “Z”] and [4, 5] becomes [[“X”, “Y”, “Z”], [4, 5]].
Note that [[“X”, “Y”, “Z”], [4, 5]] is actually a very interesting data shape! It is a table where the first row has three elements while the second only two. This means that using nested lists is more flexible than using spreadsheets or mathematical matrices where the shapes are always rectangular or square, say 2×3. These uneven sized lists of lists we also call jagged lists.
Geometrically, “merge” causes two point lists to consolidate into one polyline, while “entwine” retains two chains ie. produces separate polylines.
We looked at the “list item” component’s use of retrieving items from lists using their index. Its behaviour for nested lists is rather peculiar or a bit non-standard compared with general programming languages so tread carefully.
Observe below that requesting the 0th element from a table, selects the 0th element of every sub-list e.g. for [[“X4”, “X5”], [“Y4”, “Y5”], [“Z4”, “Z5”]] we therefore obtain a table [[“X4”], [“Y4”], [“Z4”]]. You may consider of this as selecting the 0th row.
To get the 1st element, we first need to flatten the result and then use another “list item” component e.g. flatten [[“X4”], [“Y4”], [“Z4”]] results to [“X4”, “Y4”, “Z4”] and selection at index 1 results to “Y4”. To select columns instead of rows we just use transpose first and preform the same operations.
Geometry-wise we can see that selecting by index of a table produces rows when chained using the polyline component. Note we need to use the flatten modifier on the output before connecting to the “polyline” node.
Using two “list item” components allows us to select a single point from the grid. To visualize the selection we attached here a circle as indicator.
No matter the peculiarity of this process it is reasonable to expect that for a matrix or a spreadsheet, we need to two indices to retrieve one value i.e. both a column and a row number. In math we use the notation M4,5 for selecting the element at the fourth row, fifth column of matrix M, in spreadsheets we use the equivalent M!D5, whereas in computing we use M[3][4] (we start from zero).