在Python中转换多维数组维度



是否可以转换此:

[ [0], [1], [2], [3], [4], [5], [[6,9]], [7], [8], [[6,9]] ]

进入这个:

[ [0], [1], [2], [3], [4], [5], [6,9], [7], [8], [6,9] ]
[item if isinstance(item, list) else [item] for items in data for item in items]
# [[0], [1], [2], [3], [4], [5], [6, 9], [7], [8], [6, 9]]

你可以做:

x = [i[0] if (type(i[0])==list and len(i[0])>1) else i for i in a]

示例

>>> a = [ [0], [1], [2], [3], [4], [5], [[6,9]], [7], [8], [[6,9]] ]
>>> x = [i[0] if (type(i[0])==list and len(i[0])>1) else i for i in a]
>>> x
[[0], [1], [2], [3], [4], [5], [6, 9], [7], [8], [6, 9]]

您可以这样做:

>>> data = [ [0], [1], [2], [3], [4], [5], [[6,9]], [7], [8], [[6,9]] ]
>>> o_data = [a[0] if isinstance(a[0], list) else a for a in data]
>>> o_data
[[0], [1], [2], [3], [4], [5], [6, 9], [7], [8], [6, 9]]

相关内容

  • 没有找到相关文章

最新更新