下标序列时,Python 中的 ::(双冒号)是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3453085/
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
What is :: (double colon) in Python when subscripting sequences?
提问by Aillyn
I know that I can use something like string[3:4]to get a substring in Python, but what does the 3 mean in somesequence[::3]?
我知道我可以使用类似的东西string[3:4]在 Python 中获取一个子字符串,但是 3 是什么意思somesequence[::3]?
采纳答案by Adriano Varoli Piazza
it means 'nothing for the first argument, nothing for the second, and jump by three'. It gets every third item of the sequence sliced. Extended slicesis what you want. New in Python 2.3
它的意思是“第一个参数什么都没有,第二个参数什么都没有,然后跳三个”。它对序列的每第三个项目进行切片。 扩展切片正是您想要的。Python 2.3 中的新功能
回答by Justin Ethier
When slicing in Python the third parameter is the step. As others mentioned, see Extended Slicesfor a nice overview.
在 Python 中切片时,第三个参数是步骤。正如其他人提到的,请参阅扩展切片以获得很好的概述。
With this knowledge, [::3]just means that you have not specified any start or end indices for your slice. Since you have specified a step, 3, this will take every third entry of somethingstarting at the first index. For example:
有了这些知识,[::3]就意味着您还没有为切片指定任何开始或结束索引。由于您指定了一个步骤 ,3这将从something第一个索引开始每隔三个条目进行一次。例如:
>>> '123123123'[::3]
'111'
回答by deinst
Python sequence slice addresses can be written as a[start:end:step] and any of start, stop or end can be dropped. a[::3]is every third element of the sequence.
Python 序列切片地址可以写为 [start:end:step] 并且可以删除任何开始、停止或结束。 a[::3]是序列的每三个元素。
回答by Yuval Adam
seq[::n]is a sequence of each n-th item in the entire sequence.
seq[::n]是整个序列中每项n的序列。
Example:
例子:
>>> range(10)[::2]
[0, 2, 4, 6, 8]
The syntax is:
语法是:
seq[start:end:step]
So you can do:
所以你可以这样做:
>>> range(100)[5:18:2]
[5, 7, 9, 11, 13, 15, 17]
回答by mshafrir
The third parameter is the step. So [::3] would return every 3rd element of the list/string.
第三个参数是步长。所以 [::3] 将返回列表/字符串的每个第三个元素。
回答by Bolo
Explanation
解释
s[i:j:k]is, according to the documentation, "slice of s from i to j with step k". When iand jare absent, the whole sequence is assumed and thus s[::k]means "every k-th item".
s[i:j:k]是,根据文档“从I S的切片与步骤k J”。当i和j不存在时,假设整个序列,因此s[::k]意味着“每个第 k 项”。
Examples
例子
First, let's initialize a list:
首先,让我们初始化一个列表:
>>> s = range(20)
>>> s
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Let's take every 3rditem from s:
让我们每3次项目从s:
>>> s[::3]
[0, 3, 6, 9, 12, 15, 18]
Let's take every 3rditem from s[2:]:
让我们每3次项目从s[2:]:
>>> s[2:]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> s[2::3]
[2, 5, 8, 11, 14, 17]
Let's take every 3rditem from s[5:12]:
让我们每3次项目从s[5:12]:
>>> s[5:12]
[5, 6, 7, 8, 9, 10, 11]
>>> s[5:12:3]
[5, 8, 11]
Let's take every 3rditem from s[:10]:
让我们每3次项目从s[:10]:
>>> s[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s[:10:3]
[0, 3, 6, 9]
回答by POWERRANGERDUDEONCAPS
Python uses the :: to separate the End, the Start, and the Step value.
Python 使用 :: 来分隔 End、Start 和 Step 值。
回答by Atlas7
TL;DR
TL; 博士
This visual example will show you how to a neatly select elements in a NumPy Matrix (2 dimensional array) in a pretty entertaining way (I promise). Step 2 below illustrate the usage of that "double colons" ::in question.
这个可视化示例将向您展示如何以一种非常有趣的方式(我保证)整齐地选择 NumPy 矩阵(二维数组)中的元素。下面的第 2 步说明了有::问题的“双冒号”的用法。
(Caution: this is a NumPy array specific example with the aim of illustrating the a use case of "double colons" ::for jumping of elements in multiple axes. This example does not cover native Python data structures like List).
(注意:这是一个 NumPy 数组特定示例,目的是说明“双冒号”::用于在多个轴上跳转元素的用例。此示例不包括像 那样的原生 Python 数据结构List)。
One concrete example to rule them all...
一个具体的例子来统治他们......
Say we have a NumPy matrix that looks like this:
假设我们有一个 NumPy 矩阵,如下所示:
In [1]: import numpy as np
In [2]: X = np.arange(100).reshape(10,10)
In [3]: X
Out[3]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
Say for some reason, your boss wants you to select the following elements:
假设出于某种原因,您的老板希望您选择以下元素:
"But How???"... Read on! (We can do this in a 2-step approach)
“但是如何???”...继续阅读!(我们可以分两步完成)
Step 1 - Obtain subset
步骤 1 - 获取子集
Specify the "start index" and "end index" in both row-wise and column-wise directions.
在行和列方向上指定“开始索引”和“结束索引”。
In code:
在代码中:
In [5]: X2 = X[2:9,3:8]
In [6]: X2
Out[6]:
array([[23, 24, 25, 26, 27],
[33, 34, 35, 36, 37],
[43, 44, 45, 46, 47],
[53, 54, 55, 56, 57],
[63, 64, 65, 66, 67],
[73, 74, 75, 76, 77],
[83, 84, 85, 86, 87]])
Notice now we've just obtained our subset, with the use of simple start and end indexing technique. Next up, how to do that "jumping"... (read on!)
请注意,现在我们刚刚使用简单的开始和结束索引技术获得了我们的子集。接下来,如何进行“跳跃”......(继续阅读!)
Step 2 - Select elements (with the "jump step" argument)
第 2 步 - 选择元素(使用“跳转步骤”参数)
We can now specify the "jump steps" in both row-wise and column-wise directions (to select elements in a "jumping" way) like this:
我们现在可以在行方向和列方向(以“跳跃”方式选择元素)指定“跳转步骤”,如下所示:
In code (note the double colons):
在代码中(注意双冒号):
In [7]: X3 = X2[::3, ::2]
In [8]: X3
Out[8]:
array([[23, 25, 27],
[53, 55, 57],
[83, 85, 87]])
We have just selected all the elements as required! :)
我们刚刚根据需要选择了所有元素!:)
?Consolidate Step 1 (start and end) and Step 2 ("jumping")
?巩固第1步(开始和结束)和第2步(“跳跃”)
Now we know the concept, we can easily combine step 1 and step 2 into one consolidated step - for compactness:
现在我们知道了这个概念,我们可以轻松地将第 1 步和第 2 步合并为一个合并的步骤 - 为了紧凑:
In [9]: X4 = X[2:9,3:8][::3,::2]
In [10]: X4
Out[10]:
array([[23, 25, 27],
[53, 55, 57],
[83, 85, 87]])
Done!
完毕!


