【问题标题】:TypeError: 'GitHubIterator' object does not support indexing [duplicate]TypeError:'GitHubIterator'对象不支持索引[重复]
【发布时间】:2016-11-30 21:39:58
【问题描述】:

使用 github3.py,我想检索与拉取请求关联的 cmets 列表中的最后一条评论,然后在其中搜索字符串。我已经尝试了下面的代码,但我得到了错误TypeError: 'GitHubIterator' object does not support indexing(没有索引,我可以检索 cmets 列表)。

for comments in list(GitAuth.repo.issue(prs.number).comments()[-1]):
    if sign_off_regex_search_string.search(comments.body) and comments.user.login == githubID:
        sign_off_by_author_search_string_found = 'True'
        break

【问题讨论】:

    标签: python indexing github3.py


    【解决方案1】:

    我很确定你的代码的第一行没有做你想做的事。您正在尝试索引(使用[-1])一个不支持索引的对象(它是某种迭代器)。您还围绕它进行了一个列表调用,并在该列表上运行了一个循环。我认为你不需要循环。试试:

    comments = list(GitAuth.repo.issue(prs.number).comments())[-1]
    

    我已将 list 调用中的右括号移到索引之前。这意味着索引发生在列表上,而不是迭代器上。然而,它确实浪费了一点内存,因为在我们索引最后一个并丢弃列表之前,所有的 cmets 都存储在一个列表中。如果内存使用是一个问题,您可以恢复循环并摆脱 list 调用:

    for comments in GitAuth.repo.issue(prs.number).comments():
        pass # the purpose of this loop is to get the last `comments` value
    

    其余代码不应在此循环内。循环变量comments(应该是comment,因为它指的是单个项目)在循环结束后仍将绑定到来自迭代器的最后一个值。这就是您要搜索的内容。

    【讨论】:

      猜你喜欢
      • 2013-08-23
      • 2018-01-07
      • 2013-06-23
      • 2019-04-18
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多