在Python中转换多维数组维度

时间:2014-04-13 03:56:24

标签: python multidimensional-array

是否可以转换它:

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

进入这个:

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

3 个答案:

答案 0 :(得分:2)

你可以这样做:

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]]

答案 1 :(得分:2)

[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]]

答案 2 :(得分:0)

你可以这样做:

>>> 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]]