Python-从列表列表中删除列
在列表列表中,每个子列表的相同索引处的元素表示类似列的结构。在本文中,我们将看到如何从列表列表中删除一列。这意味着我们必须从每个子列表中删除相同索引位置的元素。
使用流行音乐
我们使用pop方法删除特定位置的元素。for循环旨在遍历特定索引处的元素,并使用pop删除它们。
示例
# List of lists
listA = [[3, 9, 5, 1],
[4, 6, 1, 2],
[1, 6, 12, 18]]
# printing original list
print("Given list \n",listA)
# Apply pop
[i.pop(2) for i in listA]
# Result
print("List after deleting the column :\n ",listA)输出结果
运行上面的代码给我们以下结果-
Given list [[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]] List after deleting the column : [[3, 9, 1], [4, 6, 2], [1, 6, 18]]
与德尔
在这种方法中,我们使用与上述方法类似的del函数。我们提到了必须删除该列的索引。
示例
# List of lists
listA = [[3, 9, 5, 1],
[4, 6, 1, 2],
[1, 6, 12, 18]]
# printing original list
print("Given list \n",listA)
# Apply del
for i in listA:
del i[2]
# Result
print("List after deleting the column :\n ",listA)输出结果
运行上面的代码给我们以下结果-
Given list [[3, 9, 5, 1], [4, 6, 1, 2], [1, 6, 12, 18]] List after deleting the column : [[3, 9, 1], [4, 6, 2], [1, 6, 18]]
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西