如何切割Python列表,以便将列移动为单独的元素列?

时间:2015-12-05 06:17:24

标签: python list slice

我有以下表格的清单:

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

我想切出第一列并将其作为新元素添加到每行数据(因此在列表中的每个奇数位置),将其更改为以下形式:

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

我怎么能这样做?

到目前为止,我已通过以下方式提取了必要的信息:

targets = [element[0] for element in dataset]
features = dataset[1:]

5 个答案:

答案 0 :(得分:5)

尝试索引然后得到扁平列表 - 我使用列表理解进行展平。

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

答案 1 :(得分:4)

列表理解很好但可能有点难以扫描。循环仍然有用,特别是与extend结合使用时

res = []
for entry in dataset:
    res.extend([entry[1:], entry[:1]])

现在:

import pprint    
pprint.pprint(res)

打印:

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

答案 2 :(得分:2)

切片每个子列表并为每个切片创建一个带有元素的新list

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

>>> print(*[item for sub in l for item in (sub[1:], [sub[0]])], sep='\n')
[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]

答案 3 :(得分:2)

试试这个:

from itertools import chain
print list(chain(*[list((element[1:],[element[0]])) for element in a]))

输出:

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

答案 4 :(得分:1)

python 3.X中的Pythonic方法,使用解包迭代和itertools.chain

>>> from itertools import chain
>>> 
>>> list(chain.from_iterable([[j,[i]] for i,*j in A]))
[[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]]