简体   繁体   English

在Python 3中删除列表中每个列表的最后一个元素

[英]Remove the last element each list of list in Python 3

I would like to know how I can remove the last element of each list, which is stored in a list. 我想知道如何删除存储在列表中的每个列表的最后一个元素。 I have this example: 我有这个例子:

a=[ [1, 2, 3, 4, ''], [ 4, 5, 6, 7, ''], [ 8, 8, 8, 8, '']]

And I want to remove the last element of each list: 我想删除每个列表的最后一个元素:

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

I have tried to use a map function and filter function, but they only work in a single list, not in a list of list. 我尝试使用map函数和filter函数,但是它们仅在单个列表中起作用,而不在列表列表中起作用。

Any ideas on this? 有什么想法吗?

Thank you 谢谢

You can use Explain Python's slice notation and a list comprehension : 您可以使用Explain Python的切片符号列表推导

>>> a = [[1, 2, 3, 4, ''], [4, 5, 6, 7, ''], [8, 8, 8, 8, '']]
>>> a_removed = [x[:-1] for x in a]
>>> a_removed
[[1, 2, 3, 4], [4, 5, 6, 7], [8, 8, 8, 8]]
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM