Python 如何截断列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4838504/
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-18 17:44:57  来源:igfitidea点击:

How do I truncate a list?

pythonlist

提问by Ambrosio

If I have a list and want to truncate it so it is no more than 100 items, how do I do this?

如果我有一个列表并想将其截断使其不超过 100 个项目,我该怎么做?

采纳答案by Ned Batchelder

To modify the list in place (rather than make a shorter copy of the list), use:

要就地修改列表(而不是制作更短的列表副本),请使用:

del l[100:]

回答by GWW

You can use list slicing:

您可以使用列表切片:

a = a[0:100]

回答by Bryan Ward

You can do something like:

您可以执行以下操作:

truncated = list[:100]

回答by Bryan Ward

The items[:100]other mentioned gives you a new list which contains the first 100 items of items. If you want to modify the list in-place, either use items[:] = items[:100](slice assignment) or while len(items) > 100: items.pop()use del items[100:]as proposed by Ned Batchelder.

items[:100]提到的另一个为您提供了一个新列表,其中包含items. 如果要就地修改列表,使用items[:] = items[:100](切片分配)或while len(items) > 100: items.pop()del items[100:]按照 Ned Batchelder 的建议使用。

回答by whaley

You can use slicing if you don't mind just simply creating a new copy of the list that contains only the elements you want... however this leaves the original list unmodified.

如果您不介意简单地创建仅包含您想要的元素的列表的新副本,则可以使用切片……但是这不会修改原始列表。

>>> a = [0,1,2,3,4,5,6,7,8,9]
>>> b = a[0:5]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b
[0, 1, 2, 3, 4]

If you really want to truncate the original list, just delete the elements you don't want by using slicing with del

如果您真的想截断原始列表,只需使用带有del 的切片删除您不想要的元素

>>> del a[5:]
>>> a
[0, 1, 2, 3, 4]

回答by Kevin J. Rice

The correct answer is, of course:

正确答案当然是:

>>> x = range(10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x = x[:5]
>>> x
[0, 1, 2, 3, 4]

But, importantly, if you're interested in the values above 100 and want to pull them off one by one for whatever reason, you can also use POP()

但是,重要的是,如果您对 100 以上的值感兴趣,并且出于任何原因想要将它们一一拉下,您也可以使用POP()

>>> x = range(10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x.pop()
9
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8]

You can even specify which element to pull out:

您甚至可以指定要拉出的元素:

>>> x= range(10,20)
>>> x
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> x.pop(3)
13
>>> x.pop()
19
>>> x
[10, 11, 12, 14, 15, 16, 17, 18]
>>> x.pop(-1)
18
[10, 11, 12, 14, 15, 16, 17]

This removes individual elements, shortening the list, without copying.

这将删除单个元素,缩短列表,而无需复制。

So, an obtuse and yucky answer (but also correct) would be to iterate down. I'm only going down from 12 to 8 for ease of reading here:

因此,一个迟钝而令人讨厌的答案(但也是正确的)是向下迭代。为了便于阅读,我只从 12 降到 8:

>>> x=range(12)
>>> for i in range(len(x), 8, -1):
...     y = x.pop()
...     print "popping x: ", y, ", x=", x
...
popping x:  11 , x= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
popping x:  10 , x= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
popping x:  9 , x= [0, 1, 2, 3, 4, 5, 6, 7, 8]
popping x:  8 , x= [0, 1, 2, 3, 4, 5, 6, 7]

Sure, it's not optimal, but I just ran into a situation where I needed this so I thought I'd share it here (I'm truncating a list when I see the first not-None value).

当然,它不是最佳的,但我刚遇到需要它的情况,所以我想我会在这里分享它(当我看到第一个 not-None 值时,我正在截断一个列表)。