简体   繁体   English

python 的新手:这是索引和切片吗?

[英]New to python: is this indexing and slicing?

new to python. python 的新手。 Learn indexing and slicing.学习索引和切片。 what does it mean.这是什么意思。

for i in range (len(number)):
    if(number[-i-1]=='0'):

i don't understand this [-i-1]我不明白这个 [-i-1]

the number I'm assuming is a list of numbers eg.我假设的数字是一个数字列表,例如。 [0,1,2,3,4] [0,1,2,3,4]

the number[-i-1] refers to the number at the index of -i-1 (indexing) number[-i-1] 指的是 -i-1 索引处的数字(索引)

for example when i = 0, -i-1 = -1, which in python means the last index or 1 from the back例如,当 i = 0 时,-i-1 = -1,在 python 中表示最后一个索引或从后面算起的 1

which in my example would be 4在我的例子中是 4

Slicing would be like numbers[2:4], which will return a portion of the array of [2,3]切片就像 numbers[2:4],它将返回数组的一部分 [2,3]

What you are showing is indexing say your array is你所展示的是索引说你的数组是

number=[2,4,6,8,10]
number[0] is 2
number[-1] is 10

So, If you want to get the ith number from front the indexing is所以,如果你想从前面获取第i 个数字,索引是

number[i]

If you want to get the ith number from rear,which is your case then the indexing looks like如果你想从后面获取第i 个数字,这是你的情况,那么索引看起来像

number[-i-1]

Just for your information slicing is used to get a part of the sequence in the array仅供参考,切片用于获取数组中序列的一部分

number[1:4] gives [4,6,8] 
number[:4] gives [2,4,6,8]
number[4:] gives [10]

Hope this helps希望这可以帮助

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

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