第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以返回在任何迭代中找到的值嗎?

我可以返回在任何迭代中找到的值嗎?

達令說 2022-01-18 17:54:02
我正在自學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
查看完整描述

2 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

不是現(xiàn)在,但您將能夠在 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)

如果你想捕獲所有可能出現(xiàn)的禁用詞,你不能使用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)

)


查看完整回答
反對 回復 2022-01-18
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

更改此行


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引發(fā)異常。您可以捕獲在您的情況下不存在時會發(fā)生的異常xLuser.screen_name.lower()config.banned_name_keywords


查看完整回答
反對 回復 2022-01-18
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號