📌  相关文章
📜  Python|将列转换为列表列表中的单独元素

📅  最后修改于: 2022-05-13 01:55:02.055000             🧑  作者: Mango

Python|将列转换为列表列表中的单独元素

在某些情况下,我们可能需要提取矩阵的特定列并将其每个值分配为列表中的单独实体,这通常在机器学习领域具有实用性。让我们讨论可以执行此操作的某些方式。
方法#1:使用列表切片和列表推导
列表切片和理解的功能可以结合起来执行从列表中提取列的特定任务,然后可以使用列表理解将其添加为新元素。

Python3
# Python3 code to demonstrate 
# column to separate elements in list of lists
# using list slicing and list comprehension
 
# initializing list of list
test_list = [[1, 3, 4],
             [6, 2, 8],
             [9, 10, 5]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list slicing and list comprehension
# column to separate elements in list of lists
res = [i for nest_list in [[j[1 : ], [j[0]]]
         for j in test_list] for i in nest_list]
 
# printing result
print ("The list after column shift is : " + str(res))


Python3
# Python3 code to demonstrate 
# column to separate elements in list of lists
# using itertools.chain()+ list comprehension + list slicing
from itertools import chain
 
# initializing list of list
test_list = [[1, 3, 4],
             [6, 2, 8],
             [9, 10, 5]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using itertools.chain() + list comprehension + list slicing
# column to separate elements in list of lists
res = list(chain(*[list((sub[1: ], [sub[0]]))
                      for sub in test_list]))
 
# printing result
print ("The list after column shift is : " + str(res))


输出
The original list is : [[1, 3, 4], [6, 2, 8], [9, 10, 5]]
The list after column shift is : [[3, 4], [1], [2, 8], [6], [10, 5], [9]]


方法 #2:使用 itertools.chain() + 列表理解 + 列表切片
可以通过引入元素链接的概念并减少列表理解的开销并减少执行此特定任务所花费的时间来改进上述方法。

Python3

# Python3 code to demonstrate 
# column to separate elements in list of lists
# using itertools.chain()+ list comprehension + list slicing
from itertools import chain
 
# initializing list of list
test_list = [[1, 3, 4],
             [6, 2, 8],
             [9, 10, 5]]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using itertools.chain() + list comprehension + list slicing
# column to separate elements in list of lists
res = list(chain(*[list((sub[1: ], [sub[0]]))
                      for sub in test_list]))
 
# printing result
print ("The list after column shift is : " + str(res))
输出
The original list is : [[1, 3, 4], [6, 2, 8], [9, 10, 5]]
The list after column shift is : [[3, 4], [1], [2, 8], [6], [10, 5], [9]]