简体   繁体   English

切片与索引

[英]Slicing vs indexing

I thought Python (numpy) is zero indexing, but when I slice [:0] it returns empty array.我认为 Python (numpy) 是零索引,但是当我切片[:0]它返回空数组。 I thought I'm saying slice from zero to zero but clearly I am not.我以为我说的是从零到零的切片,但显然我不是。

If instead I use A[1] it returns the position 1 element of by zero-indexing.相反,如果我使用A[1]它会通过零索引返回位置 1 的元素。

When using slice it excludes the endpoint, just like in range(a, b) = a..(b-1)使用 slice 时,它​​会排除端点,就像在 range(a, b) = a..(b-1) 中一样

Though when you do list[:1] it should return [list[0]] , not an empty array.虽然当你做list[:1]它应该返回[list[0]] ,而不是一个空数组。 Thus I suspect that your array is empty from the beginning.因此我怀疑你的数组从一开始就是空的。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM