Returns the elements of list l structured according to the given structure. A structure is represented by a list whose elements are either `None` or a non-negative integer. `None` corresponds to a single element in the output list, and an integer N corresponds to a nested list of length N.
(l, structure)
| 90 | |
| 91 | |
| 92 | def _Restructure(l, structure): |
| 93 | """Returns the elements of list l structured according to the given structure. |
| 94 | |
| 95 | A structure is represented by a list whose elements are either |
| 96 | `None` or a non-negative integer. `None` corresponds to a single |
| 97 | element in the output list, and an integer N corresponds to a nested |
| 98 | list of length N. |
| 99 | |
| 100 | The function returns a data structure whose shape is given by |
| 101 | `structure`, and whose elements are taken from `l`. If `structure` |
| 102 | is a singleton, the function returns the single data structure |
| 103 | implied by the 0th element of `structure`. For example: |
| 104 | |
| 105 | _Restructure(["foo", "bar", "baz", "qux"], [None, 2, None]) |
| 106 | -> ["foo", ["bar", "baz"], "qux"] |
| 107 | |
| 108 | _Restructure(["foo"], [None]) -> "foo" |
| 109 | |
| 110 | _Restructure(["foo"], [1]) -> ["foo"] |
| 111 | |
| 112 | _Restructure([], [0]) -> [] |
| 113 | |
| 114 | Args: |
| 115 | l: A list. |
| 116 | structure: A list whose elements are either `None` or a non-negative |
| 117 | integer. |
| 118 | |
| 119 | Returns: |
| 120 | The elements of `l`, restructured according to `structure`. If |
| 121 | `structure` is a list of length 1, this function returns the |
| 122 | single data structure implied by `structure[0]`. |
| 123 | |
| 124 | """ |
| 125 | result = [] |
| 126 | current_index = 0 |
| 127 | for element in structure: |
| 128 | if element is None: |
| 129 | result.append(l[current_index]) |
| 130 | current_index += 1 |
| 131 | else: |
| 132 | result.append(l[current_index:current_index+element]) |
| 133 | current_index += element |
| 134 | |
| 135 | if len(result) == 1: |
| 136 | return result[0] |
| 137 | else: |
| 138 | return tuple(result) |
| 139 | |
| 140 | |
| 141 | def _MakeFloat(v, arg_name): |