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 敏字的祝福语简短
8 入学校简短祝福语
10 生日祝福语简短蛋糕上
11 小红书生日祝福语简短
12 祝女儿出嫁简短祝福语
13 生日寿宴祝福语简短精辟
14 祝福语简短唯美10字
15 感动妈妈的祝福语简短
16 六一加端午祝福语简短
17 对同事简短生日祝福语
18 给男生的简短祝福语