从 Python 中的嵌套列表中删除一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22800028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:43:43  来源:igfitidea点击:

Remove a column from a nested list in Python

pythonlist

提问by Crisis

I need help figuring how to work around removing a 'column' from a nested list to modify it.

我需要帮助弄清楚如何解决从嵌套列表中删除“列”以修改它的问题。

Say I have

说我有

L = [[1,2,3,4],
     [5,6,7,8],
     [9,1,2,3]]

and I want to remove the second column (so values 2,6,1) to get:

我想删除第二列(因此值为 2,6,1)以获得:

L = [[1,3,4],
     [5,7,8],
     [9,2,3]]

I'm stuck with how to modify the list with just taking out a column. I've done something sort of like this before? Except we were printing it instead, and of course it wouldn't work in this case because I believe the break conflicts with the rest of the values I want in the list.

我被困在如何通过取出一列来修改列表。我以前做过类似的事情吗?除了我们打印它之外,当然在这种情况下它不起作用,因为我相信 break 与列表中我想要的其余值冲突。

def L_break(L):

i = 0
while i < len(L):
    k = 0
    while k < len(L[i]):
        print( L[i][k] , end = " ")
        if k == 1:
            break
        k = k + 1
    print()
    i = i + 1

So, how would you go about modifying this nested list? Is my mind in the right place comparing it to the code I have posted or does this require something different?

那么,您将如何修改这个嵌套列表?将它与我发布的代码进行比较,我的想法是否正确,或者这是否需要不同的东西?

回答by arshajii

You can simply delete the appropriate element from each row using del:

您可以使用以下命令简单地从每一行中删除适当的元素del

L = [[1,2,3,4],
     [5,6,7,8],
     [9,1,2,3]]

for row in L:
    del row[1]  # 0 for column 1, 1 for column 2, etc.

print L
# outputs [[1, 3, 4], [5, 7, 8], [9, 2, 3]]

回答by kojiro

If you want to extractthat column for later use, while removing it from the original list, use a list comprehension with pop:

如果您想提取该列以供以后使用,同时将其从原始列表中删除,请使用列表推导式pop

>>> L = [[1,2,3,4],
...       [5,6,7,8],
...       [9,1,2,3]]
>>> 
>>> [r.pop(1) for r in L]
[2, 6, 1]
>>> L
[[1, 3, 4], [5, 7, 8], [9, 2, 3]]

Otherwise, just loop over the list and delete the fields you no longer want, as in arshajii's answer

否则,只需遍历列表并删除您不再需要的字段,如arshajii 的回答

回答by Hai Vu

Here is one way, updated to take in kojiro's advice.

这是一种方法,更新以采纳 kojiro 的建议。

>>> L[:] = [i[:1]+i[2:] for i in L]
>>> L
[[1, 3, 4], [5, 7, 8], [9, 2, 3]]

You can generalize this to remove any column:

您可以将其概括为删除任何列:

def remove_column(matrix, column):
    return [row[:column] + row[column+1:] for row in matrix]

# Remove 2nd column
copyofL = remove_column(L, 1) # Column is zero-base, so, 1=second column

回答by thefourtheye

You can use operator.itemgetter, which is created for this very purpose.

您可以使用operator.itemgetter专为此目的而创建的 。

from operator import itemgetter
getter = itemgetter(0, 2, 3)            # Only indexes which are needed
print(list(map(list, map(getter, L))))
# [[1, 3, 4], [5, 7, 8], [9, 2, 3]]

You can use it in List comprehension like this

您可以像这样在列表理解中使用它

print([list(getter(item)) for item in L])
# [[1, 3, 4], [5, 7, 8], [9, 2, 3]]

You can also use nested List Comprehension, in which we skip the elements if the index is 1, like this

您还可以使用嵌套列表理解,如果索引为 1,我们跳过元素,就像这样

print([[item for index, item in enumerate(items) if index != 1] for items in L])
# [[1, 3, 4], [5, 7, 8], [9, 2, 3]]

Note:All these suggested in this answer will not affect the original list. They will generate new lists without the unwanted elements.

注意:此答案中建议的所有这些都不会影响原始列表。他们将生成没有不需要的元素的新列表。

回答by idanshmu

Use map-lambda:

使用map-lambda

print map(lambda x: x[:1]+x[2:], L)

回答by Shinto Joseph

when you do the delit will delete that index and reset the index, so you have to reduce that index. Here I use the count to reduce and reset the same from the index list we have. Hope this helps. Thanks

当您执行del 时,它将删除该索引并重置该索引,因此您必须减少该索引。在这里,我使用计数来减少和重置我们拥有的索引列表中的计数。希望这可以帮助。谢谢

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
remove_cols_index = [1,2]

count = 0
for i in remove_cols_index:
    i = i-count
    count = count+1

    del nested_list[i]

print (nested_list)