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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

達(dá)令說(shuō) 2022-01-18 17:54:02
我正在自學(xué)python,并且正在對(duì)其他人編寫的機(jī)器人進(jìn)行一些更改。我正在嘗試為 NSFW 或不適合兒童的單詞添加過(guò)濾器。我已將這些詞添加到名為 config.banned_name_keywords 的列表中。我最初通過(guò)返回整個(gè)推文使其正常工作,但我試圖返回找到的特定單詞,以便我可以排除故障并編輯列表。我可以使用 tweet.text 返回整個(gè)推文,但這會(huì)影響輸出并阻塞屏幕。我也試過(guò) 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貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

不是現(xiàn)在,但您將能夠在 Python 3.8 中通過(guò)賦值表達(dá)式:


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,因?yàn)樗哪康氖窃谀阏业揭粋€(gè)匹配項(xiàng)時(shí)立即停止。為此,您只想計(jì)算交集(您今天也可以這樣做):


banned = set(config.banned_name_keywords)


...


else:

    caught = banned.intersection(tweet.text.lower().split())

    if caught:

        print("Avoided tweet with banned words: " + caught)

(它本身也可以使用賦值表達(dá)式來(lái)縮短:


elif (caught := banned.intersection(tweet.text.lower().split())):

    print("Avoided tweet with banned words: " + caught)

)


查看完整回答
反對(duì) 回復(fù) 2022-01-18
?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

更改此行


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函數(shù)返回列表中的索引,如果列表中不存在,則L引發(fā)異常。您可以捕獲在您的情況下不存在時(shí)會(huì)發(fā)生的異常xLuser.screen_name.lower()config.banned_name_keywords


查看完整回答
反對(duì) 回復(fù) 2022-01-18
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)