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]]
热门推荐
10 学校职工祝福语大全简短
11 虎年新年专属祝福语简短
12 过节文案成语祝福语简短
13 孙女结婚姥姥祝福语简短
14 端午发给客户祝福语简短
15 蛋糕生日弥勒祝福语简短
16 六一互换礼物祝福语简短
17 祝寿时祝福语简短精辟
18 描写周末的祝福语简短