什么是最好的方式来转换这样的恩达雷:
[[1,2,3], [4,5,6]]致:
[[[1],[2],[3]], [[4],[5],[6]]]只需将每个值包装为数组即可。
发布于 2016-09-29 08:11:10
你可以在最后引入一个带有np.newaxis/None的新轴,就像-
arr[...,None]样本运行-
In [6]: arr = np.array([[1,2,3], [4,5,6]])
In [7]: arr[...,None]
Out[7]:
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]])
In [8]: arr[...,None].tolist() # To show it as a list for expected o/p format
Out[8]: [[[1], [2], [3]], [[4], [5], [6]]]发布于 2016-09-29 08:15:37
您可以通过递归地浏览列表列表来做到这一点:
def wrap_values(list_of_lists):
if isinstance(list_of_lists, list):
return [wrap_values(el) for _,el in enumerate(list_of_lists)]
else:
return [list_of_lists]
xx = [[1,2,3], [4,5,6]]
yy = wrap_values(xx) # [[[1], [2], [3]], [[4], [5], [6]]]https://stackoverflow.com/questions/39764773
复制相似问题