负面清单索引? [英] Negative list index?

查看:50
本文介绍了负面清单索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
解释切片符号

Possible Duplicate:
Explain slice notation

我正在尝试理解以下代码:

I'm trying to understand the following piece of code:

# node list
n = []
for i in xrange(1, numnodes + 1):
    tmp = session.newobject();
    n.append(tmp)
link(n[0], n[-1])

具体来说,我不理解索引-1指的是什么.如果索引0引用第一个元素,那么-1引用什么?

Specifically, I don't understand what the index -1 refers to. If the index 0 refers to the first element, then what does -1 refer to?

推荐答案

负数表示您从右数而不是从左数.因此,list[-1]指的是最后一个元素,list[-2]指的是倒数第二个,依此类推.

Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.

这篇关于负面清单索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆