【问题标题】:Can I return value found in this any iteration?我可以返回在任何迭代中找到的值吗?
【发布时间】:2019-09-11 23:55:21
【问题描述】:

我正在自学 Python,并且正在对其他人编写的机器人进行一些更改。

我正在尝试为 NSFW 或不适合儿童的单词添加过滤器。我已将这些词添加到名为 config.banned_name_keywords 的列表中。

我最初通过返回整个推文使其正常工作,但我正在尝试返回找到的特定单词,以便我可以排除故障并编辑列表。

我可以使用 tweet.text 返回整个推文,但这会影响输出并阻塞屏幕。

我也试过 print(x) 但我不知道它是在哪里定义的。它首先返回找到推文的单词。

for tweet in searched_tweets:
    if any(rtwords in tweet.text.lower().split() for rtwords in config.retweet_tags):
        # The script only cares about contests that require retweeting. It would be very weird to not have to
        # retweet anything; that usually means that there's a link  you gotta open and then fill up a form.
        # This clause checks if the text contains any retweet_tags
        if tweet.retweeted_status is not None:
            # In case it is a retweet, we switch to the original one
            if any(y in tweet.retweeted_status.text.lower().split() for y in config.retweet_tags):
                tweet = tweet.retweeted_status
            else:
                continue
        if tweet.user.screen_name.lower() in config.banned_users or any(x in tweet.user.name.lower() for x in config.banned_name_keywords):
            # If it's the original one, we check if the author is banned
            print("Avoided user with ID: " + tweet.user.screen_name + " & Name: " + tweet.user.name)
            continue
        elif any(z in tweet.text.lower().split() for z in config.banned_name_keywords):
            # If the author isn't banned, we check for words we don't want
            print("Avoided tweet with words:" + z)
            continue

【问题讨论】:

    标签: python loops


    【解决方案1】:

    现在不行,但您可以通过assignment expressions 在 Python 3.8 中使用:

    elif any((caught := z) in tweet.text.lower().split() for z in config.banned_name_keywords):
        print("Avoided tweet with word: " + caught)
    

    如果你想捕捉所有可能出现的禁用词,你不能使用any,因为它的目的是在你找到一个匹配项后立即停止。为此,您只想计算交集(您今天也可以这样做):

    banned = set(config.banned_name_keywords)
    
    ...
    
    else:
        caught = banned.intersection(tweet.text.lower().split())
        if caught:
            print("Avoided tweet with banned words: " + caught)
    

    (它本身也可以使用赋值表达式来缩短:

    elif (caught := banned.intersection(tweet.text.lower().split())):
        print("Avoided tweet with banned words: " + caught)
    

    )

    【讨论】:

    • := 在 python 3.5 中有效吗?
    • 你能告诉我在上述代码的上下文中这会是什么样子吗?变量是否在循环内定义?
    • 向您展示what会是什么样子?第一个代码块是一旦发布了支持它的 Python 版本,您将如何使用:=;第二个版本现在适用于过去 10 年或 15 年发布的任何 Python 版本;第三个是第二个的 Python 3.8+ 特定版本。
    • 你是对的。我有一些缩进和空格/制表符错误。这行得通。谢谢!
    【解决方案2】:

    改变这一行

    if tweet.user.screen_name.lower() in config.banned_users or any(x in tweet.user.name.lower() for x in config.banned_name_keywords):
    

    try:
        # matched_banned_keyword below is the `SPECIFIC` word that matched
        matched_banned_keyword = config.banned_name_keywords[config.banned_name_keywords.index(tweet.user.name.lower())] 
    except:
        matched_banned_keyword = None
    if tweet.user.screen_name.lower() in config.banned_users or matched_banned_keyword:
        print("Avoided user with ID: " + tweet.user.screen_name + " & Name: " + tweet.user.name)
    

    L.index(x) 函数返回x 在列表L 中的索引,如果x 不在列表L 中,则引发异常。当user.screen_name.lower() 不存在于config.banned_name_keywords 中时,您可以捕获在您的情况下会发生的异常

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2011-06-27
      • 2019-05-02
      • 2015-06-17
      • 2010-09-21
      相关资源
      最近更新 更多