python 双冒号_Python:单冒号与双冒号

1586010002-jmsa.png

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"

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 is 2-dimensional array with rows like ['1' '0' '3' 'Braund, Mr. Owen Harris' 'male' '22' '1' '0' 'A/5 21171' '7.25' '' 'S']

解决方案

No, there is no difference.

See the Python documentation for slice:

From the docs: a[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).

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

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]

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.

Hope this helps.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值