尝试索引,然后得到扁平列表 - 我使用列表理解进行展平.
>>>l=[[0, 5.1, 3.5, 1.4, 0.2], [0, 4.9, 3.0, 1.4, 0.2], [0, 4.7, 3.2, 1.3, 0.2], [1, 4.6, 3.1, 1.5, 0.2], [1, 5.0, 3.6, 1.4, 0.2], [1, 5.4, 3.9, 1.7, 0.4], [1, 4.6, 3.4, 1.4, 0.3]] >>>[[i[1:],[i[0]]] for i in l]#get sliced list of lists >>>[[[5.1, 3.5, 1.4, 0.2], [0]], [[4.9, 3.0, 1.4, 0.2], [0]], [[4.7, 3.2, 1.3, 0.2], [0]], [[4.6, 3.1, 1.5, 0.2], [1]], [[5.0, 3.6, 1.4, 0.2], [1]], [[5.4, 3.9, 1.7, 0.4], [1]], [[4.6, 3.4, 1.4, 0.3], [1]]] >>>d=[[i[1:],[i[0]]] for i in l] >>>[item for sublist in d for item in sublist]#flatten list d >>>[[5.1, 3.5, 1.4, 0.2], [0], [4.9, 3.0, 1.4, 0.2], [0], [4.7, 3.2, 1.3, 0.2], [0], [4.6, 3.1, 1.5, 0.2], [1], [5.0, 3.6, 1.4, 0.2], [1], [5.4, 3.9, 1.7, 0.4], [1], [4.6, 3.4, 1.4, 0.3], [1]]
只是oneliner替代 -
[item for sublist in [[i[1:],[i[0]]] for i in l] for item in sublist] #Here l is that list