MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _Restructure

Function _Restructure

tensorflow/python/framework/op_def_library.py:92–138  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

90
91
92def _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
141def _MakeFloat(v, arg_name):

Callers 1

apply_opMethod · 0.85

Calls 2

tupleFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected