Python:单冒号与双冒号 [英] Python: single colon vs double colon
问题描述
在这种情况下,单冒号和双冒号有什么区别?
data[0:,4]
与data[0::,4]
What is the difference between single and double colon in this situation?
data[0:,4]
vs data[0::,4]
women_only_stats = data[0::,4] == "female"
men_only_stats = data[0::,4] != "female"
我尝试将data[0::,4]
替换为data[0:,4]
,但没有区别.在这种情况或其他情况下有什么区别吗?
I tried to replace data[0::,4]
with data[0:,4]
and I see no difference. Is there any difference in this or another case?
data
是具有像['1' '0' '3' 'Braund, Mr. Owen Harris' 'male' '22' '1' '0' 'A/5 21171' '7.25' '' 'S']
推荐答案
否,没有区别.
请参阅Python文档中的切片:
See the Python documentation for slice:
从文档中:a[start:stop:step]
start和step参数默认为None.切片对象有 只读数据属性start,stop和step,它们仅返回 参数值(或其默认值).
The start and step arguments default to None. Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default).
在这种情况下,您要包含一个空的step
参数.
In this case, you are including an empty step
parameter.
>>> a = [1,2,3,4]
>>> a[2:]
[3,4]
>>> a[2::]
[3,4]
>>> a[2:] == a[2::]
True
并了解step
参数的实际作用:
And to understand what the step
parameter actually does:
>>> b = [1,2,3,4,5,6,7,8,9,10]
>>> b[0::5]
[1, 6]
>>> b[1::5]
[2, 7]
因此,通过将其保留为隐式None
(即通过a[2:]
或a[2::]
),您将不会以任何方式更改代码的输出.
So by leaving it to be implicitly None
(i.e., by either a[2:]
or a[2::]
), you are not going to change the output of your code in any way.
希望这会有所帮助.
这篇关于Python:单冒号与双冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!