【发布时间】:2021-12-03 12:55:18
【问题描述】:
built-in function slice 的 Python 文档是(强调我的):
class slice(stop)class slice(start, stop[, step])返回一个切片对象,表示由
range(start, stop, step)指定的索引集。start和step参数默认为None。切片对象具有只读数据属性start、stop和step,它们仅返回参数值(或其默认值)。它们没有其他明确的功能;但是它们被 NumPy 和其他第三方包使用。 使用扩展索引语法时也会生成切片对象。例如:a[start:stop:step]或a[start:stop, i]。有关返回迭代器的替代版本,请参阅itertools.islice()。
a[start:stop, i] 是什么意思?
我试过了(在 Python 3.6 中):
a = [1, 2, 3, 4, 5, 6]
a[1:3,1]
但是得到了:
TypeError: list indices must be integers or slices, not tuple
【问题讨论】:
-
根据
a表示不同的含义。如果a是一个列表,那么它是无效的,正如您所见。 -
文档不清楚,但你可以在 numpy 中完成,这意味着在 1:3 行中大致选择第 1 列
-
也许只是文档中的错字?
标签: python list slice built-in