【问题标题】:Why do python list with a single entry give the 0th element for -1 index and 0 index? [duplicate]为什么带有单个条目的python列表为-1索引和0索引提供第0个元素? [复制]
【发布时间】:2019-08-04 15:26:16
【问题描述】:

在 python 中创建一个只有一个元素的列表时,当我尝试获取 -1 索引元素而不是获取列表索引超出范围错误时,它给出了第 0 个元素。

some = ['something']
print(some[0]) # prints something
print(some[-1]) # prints something

这是一个相同的链接: https://repl.it/@hearsid/SuperiorProudShoutcast

【问题讨论】:

    标签: python


    【解决方案1】:

    负索引表示nth from the right。单元素列表的零索引与单元素列表的负单索引相同,因为第零元素与右侧的第一个元素相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 2014-06-02
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多