mdjxy63的博客

###### Python Ellipsis 的使用

This came up in another question recently. I'll elaborate on my answer from there:

Ellipsis is an object that can appear in slice notation. For example:

myList[1:2, ..., 0]

Its interpretation is purely up to whatever implements the __getitem__ function and sees Ellipsis objects there, but its main (and intended) use is in the numeric python extension, which adds a multidimensional array type. Since there are more than one dimensions, slicing becomes more complex than just a start and stop index; it is useful to be able to slice in multiple dimensions as well. E.g., given a 4x4 array, the top left area would be defined by the slice [:2,:2]:

>>> a
array([[ 1,  2,  3,  4],
[ 5,  6,  7,  8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])

>>> a[:2,:2]  # top left
array([[1, 2],
[5, 6]])

Extending this further, Ellipsis is used here to indicate a placeholder for the rest of the array dimensions not specified. Think of it as indicating the full slice [:] for all the dimensions in the gap it is placed, so for a 3d array, a[...,0] is the same as a[:,:,0] and for 4d, a[:,:,:,0], similarly, a[0,...,0] is a[0,:,:,0] (with however many colons in the middle make up the full number of dimensions in the array).

Interestingly, in python3, the Ellipsis literal (...) is usable outside the slice syntax, so you can actually write:

>>> ...
Ellipsis

Other than the various numeric types, no, I don't think it's used. As far as I'm aware, it was added purely for numpy use and has no core support other than providing the object and corresponding syntax. The object being there didn't require this, but the literal "..." support for slices did.


eg:3维array，[:,:,0]<==>[...,0],

eg:4维array,[:,:,:,0]<==>[...,0]

[0,:,:,0]<==>[0,...,0]

#### python小结之Ellipsis对象

2017-10-16 17:34:18

#### Python学习之内建常量 Ellipsis详解

2017-02-14 15:14:08

#### python笔记 使用numpy中的savez（save）保存非数组数据

2018-01-18 11:52:20

#### 31.Python的单元测试工具——doctest

2016-07-09 15:34:53

#### jquery.ellipsis 使用，多行文字末尾显示省略号

2016-12-13 10:12:40

#### CSS3属性之text-overflow:ellipsis详解

2016-03-14 14:17:35

#### 设置text-overflow: ellipsis后引起的文本对齐问题

2016-10-26 16:41:52

#### firefox中text-overflow:ellipsis的问题

2016-05-26 18:43:13

#### CSS基础：text-overflow:ellipsis溢出文本显示省略号的详细方法_CSS教程

2013-12-22 10:49:39

#### table表格中文本溢出text-overflow:ellipsis使用中td表格宽度失效问题

2017-03-29 17:40:21

## 不良信息举报

Python Ellipsis 的使用