【发布时间】:2010-10-13 09:31:32
【问题描述】:
我需要列表的最后 9 个数字,我确信有一种方法可以通过切片来实现,但我似乎无法得到它。我可以像这样得到前 9 个:
num_list[0:9]
【问题讨论】:
我需要列表的最后 9 个数字,我确信有一种方法可以通过切片来实现,但我似乎无法得到它。我可以像这样得到前 9 个:
num_list[0:9]
【问题讨论】:
您可以使用带有切片运算符的负整数来实现这一点。下面是一个使用 python CLI 解释器的例子:
>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a[-9:]
[4, 5, 6, 7, 8, 9, 10, 11, 12]
重要的一行是a[-9:]
【讨论】:
-0 是0。所以a[-0:] 返回整个a,而不是最后一个零元素[]。对于守零,您可以使用a[-n:] if n > 0 else []。
负索引将从列表末尾开始计算,因此:
num_list[-9:]
【讨论】:
Python 切片是一种非常快速的操作,它是一种快速访问部分数据的便捷方式。
从列表(或任何其他支持它的序列,如字符串)中获取最后九个元素的切片表示法如下所示:
num_list[-9:]
当我看到这个时,我将括号中的部分读作“从末尾到末尾的第 9 个”。 (其实我在心里把它缩写成“-9, on”)
完整的符号是
sequence[start:stop:step]
但是冒号告诉 Python 你给了它一个切片而不是一个常规索引。这就是为什么在 Python 2 中复制列表的惯用方式是
list_copy = sequence[:]
清除它们是:
del my_list[:]
(列表在 Python 3 中得到 list.copy 和 list.clear。)
您可能会发现将形成切片与将切片传递给list.__getitem__ 方法(that's what the square brackets do) 分开很有用。即使您不是新手,它也能让您的代码更具可读性,以便可能需要阅读您的代码的其他人更容易理解您在做什么。
但是,您不能只将一些用冒号分隔的整数分配给变量。您需要使用切片对象:
last_nine_slice = slice(-9, None)
第二个参数None 是必需的,因此第一个参数被解释为start 参数otherwise it would be the stop argument。
然后您可以将切片对象传递给您的序列:
>>> list(range(100))[last_nine_slice]
[91, 92, 93, 94, 95, 96, 97, 98, 99]
islice来自 itertools 模块的islice 是另一种获得此功能的可能高效方法。 islice 不接受否定参数,因此理想情况下,您的可迭代对象具有 __reversed__ 特殊方法 - 列表确实具有 - 所以您必须首先将列表(或使用 __reversed__ 的可迭代对象)传递给reversed.
>>> from itertools import islice
>>> islice(reversed(range(100)), 0, 9)
<itertools.islice object at 0xffeb87fc>
islice 允许对数据管道进行惰性求值,因此要实现数据,请将其传递给构造函数(如 list):
>>> list(islice(reversed(range(100)), 0, 9))
[99, 98, 97, 96, 95, 94, 93, 92, 91]
【讨论】:
最后 9 个元素可以根据需要使用 numlist[-9:] 从左到右读取,或者使用 numlist[:-10:-1] 从右到左读取。
>>> a=range(17)
>>> print a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> print a[-9:]
[8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> print a[:-10:-1]
[16, 15, 14, 13, 12, 11, 10, 9, 8]
【讨论】:
以下是获取可迭代对象的“尾部”项的几个选项:
给定
n = 9
iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
期望的输出
[2, 3, 4, 5, 6, 7, 8, 9, 10]
代码
我们使用以下任一选项获得后者的输出:
from collections import deque
import itertools
import more_itertools
# A: Slicing
iterable[-n:]
# B: Implement an itertools recipe
def tail(n, iterable):
"""Return an iterator over the last *n* items of *iterable*.
>>> t = tail(3, 'ABCDEFG')
>>> list(t)
['E', 'F', 'G']
"""
return iter(deque(iterable, maxlen=n))
list(tail(n, iterable))
# C: Use an implemented recipe, via more_itertools
list(more_itertools.tail(n, iterable))
# D: islice, via itertools
list(itertools.islice(iterable, len(iterable)-n, None))
# E: Negative islice, via more_itertools
list(more_itertools.islice_extended(iterable, -n, None))
详情
iter(iterable)。 itertools recipe。它适用于任何可迭代对象,并解决了最后一个解决方案中的迭代器问题。此配方必须手动实施,因为它没有正式包含在 itertools 模块中。more_itertools(通过> pip install more-itertools 安装);见more_itertools.tail。itertools 库的成员。请注意,itertools.islicedoes not support negative slicing。 more_itertools 中实现了另一个工具,它泛化了itertools.islice 以支持负切片;见more_itertools.islice_extended。我该用哪一个?
视情况而定。在大多数情况下,切片(选项 A,如其他答案中所述)是最简单的选项,因为它内置在语言中并支持大多数可迭代类型。对于更通用的迭代器,请使用任何剩余选项。请注意,选项 C 和 E 需要安装第三方库,一些用户可能会觉得这很有用。
【讨论】: