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 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短