繁体   English   中英

遍历列表时不在列表中的“ x”

[英]'x' not in list while iterating over list

我想做的是遍历注释列表,删除已添加到名为cText的值中的当前注释,如果cText超过9000个字符,则对同一函数进行递归调用并将其返回值添加至botComments 我也尝试使用botComments + repliesToComments(comments, author)将递归函数返回的botComments列表从递归函数合并到最上面的botComments列表,尽管我不知道我是否在做正确的事情。 奇怪的是,似乎也无法在列表中找到可以肯定引发该评论的评论,因为该评论引发了我在下面粘贴的异常。 在AP Comp-Sci课堂上,递归让我感到恐惧,尽管我希望从任何可以修复下面我的代码混乱的人那里了解我在做错什么。 谢谢 :)

这是我目前的例外情况:

Traceback (most recent call last):
  File "C:\Users\Josh\Desktop\bot.py", line 62, in <module>
    print(repliesToComments(submission.comments, submission.author.name))
  File "C:\Users\Josh\Desktop\bot.py", line 36, in repliesToComments
    botComments + repliesToComments(comments, author)
  File "C:\Users\Josh\Desktop\bot.py", line 39, in repliesToComments
    comments.remove(comment)
ValueError: list.remove(x): x not in list

这是我的代码:

def repliesToComments(comments, author):
    botComments = []
    multiReplies = False

    cText = "Here is a list of all the comments /u/" + author + ''' has submitted to top-level comments:
***
Original Comment | /u/''' + author + ''''s Reply
---------|---------
'''

    for comment in comments[:]:
        if (hasattr(comment, "replies")):
            for comment2 in comment.replies:
                if (hasattr(comment2, "author") and comment2.author.name == author):
                    oCommentLink = "[" + comment.body.replace("\n", "")[:50] + "](" + submission.permalink + "" + comment2.parent_id.replace("t1_", "") + ")..."
                    rCommentLink = "[" + comment2.body.replace("\n", "")[:50] + "](" + submission.permalink + "" + comment2.name.replace("t1_", "") + ")..."
                    if (len(cText) + len(oCommentLink + " | " + rCommentLink + "\n") > 9000): #Stop here to make sure we don't go over 10000 char limit!
                        multiReplies = True
                        cText += '''
***
[FAQ](http://pastebin.com/raw.php?i=wUGmE8X5) | Generated at ''' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " PST" + " | *This comment only shows a partial amount of OPs total replies due to character limit. The rest are shown in a reply to this comment.*"
                        botComments.append(cText)
                        botComments + repliesToComments(comments, author)
                        break
                    cText += oCommentLink + " | " + rCommentLink + "\n"
        comments.remove(comment)

    if (multiReplies == False):
        cText += '''
***
[FAQ](http://pastebin.com/raw.php?i=wUGmE8X5) | Generated at ''' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " PST"

    botComments.append(cText)
    return botComments

您递归地调用repliesToComments() ,但参数完全相同 这只会导致问题。

您的递归调用将从同一列表中删除条目,当它返回时, 这些条目仍将消失 但是,因为您遍历了列表的副本 ,所以外部调用不会被更新,而是尝试删除注释,而内部递归调用被删除。

也许您想在repliesToComments()调用repliesToComments()以获取一组不同的注释? 我怀疑您是要称其为comment.replies吗?

代码的另一个问题是您忽略了递归调用的返回值:

botComments + repliesToComments(comments, author)

这将添加列表botComments和递归调用的返回值,从而创建一个列表,然后通过不将其分配给变量将其放到地板上。 您可能要使用:

botComments += repliesToComments(comments, author)

代替。

暂无
暂无

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

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