【问题标题】:Getting the last element of a PaginatedList in Python在 Python 中获取 PaginatedList 的最后一个元素
【发布时间】:2020-09-06 13:25:52
【问题描述】:

如何获取 PaginatedList 的最后一个元素?#

(<github.PaginatedList.PaginatedList object at 0x7f1c6a492a50>).

我的代码是这样的 - a_project_column.get_cards()[-1] 错误是

"IndexError: 列表索引超出范围"

但下面的代码可以正常工作以获得所需的结果(即 > >ProjectCard(id=5161717))

a_project_column.get_cards()[0]

供参考 https://github.com/PyGithub/PyGithub/blob/master/github/PaginatedList.py

【问题讨论】:

  • 看起来这不是预期用途。当您尝试yourlist._PaginatedList__elements[-1] 时会发生什么?
  • 是的,如果您查看嵌入式 Slice 类,它不支持负索引。
  • @timgeb 它显示错误 - AttributeError : 'PaginatedList' 对象没有属性 '_PaginatedList__elements'
  • @NoahSmithYeah 现在看来。

标签: python list arraylist


【解决方案1】:

我知道这已经有一段时间了,但我找到了解决方案,我想我会为任何应该像我一样去谷歌搜索的人提供它,但无济于事:

代替

a_project_column.get_cards()[-1]

试试:

cards = a_project_column.get_cards()
final_card  = cards[cards.totalCount - 1]

【讨论】:

    【解决方案2】:

    我假设您正在尝试获取列表的最后一个元素。 您可以使用列表切片:https://docs.python.org/3/tutorial/introduction.html#lists

    你的语法有点不对(你错过了冒号)

    cards = a_project_column.get_cards()[-1:]
    

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      相关资源
      最近更新 更多