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

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

從文件中提取特定單詞

從文件中提取特定單詞

不負(fù)相思意 2021-12-26 14:46:00
我正在分析一些文本文件,每次在文件中找到該詞時(shí),我都想提取一個(gè)特定的詞。想象一下,我在文件中有“體育”,然后我想根據(jù)列表提取單詞“體育”。我有以下代碼:content = ['Sports', 'Nature', 'Football']path = filenamewith open(path) as auto:    for line in auto:        if any(x.lower() in line.lower() for x in content):            print(line)我的文本文件有以下內(nèi)容:Sports TV is the home of football videos. Complex game to follow.home of football用我的代碼打印所有帶有“體育”和“足球”的行:Sports TV is the home of football videos. home of football但我想看到以下結(jié)果:Sportsfootball如何僅打印 List 上的單詞而不是所有行?
查看完整描述

2 回答

?
翻過(guò)高山走不出你

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

列表.txt:


Sports TV is the home of football videos. 

Complex game to follow.

home of football

因此:


content = ['Sports', 'Nature', 'Football']

path = 'list.txt'


with open(path) as auto:

    print([[x.lower() for x in content if x.lower() in line.lower()] for line in auto])

輸出:


[['sports', 'football'], [], ['football']]

由于:


第 1 行有sports和football


第 2 行沒(méi)有來(lái)自?xún)?nèi)容列表的匹配元素


第 3 行有 football


查看完整回答
反對(duì) 回復(fù) 2021-12-26
?
函數(shù)式編程

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

您目前正在打印整行


嘗試:


content = ['Sports', 'Nature', 'Football']

path = filename

with open(path) as auto:

    for line in auto:

        for x in content:

            if x.lower() in line.lower():

                print(x)


查看完整回答
反對(duì) 回復(fù) 2021-12-26
  • 2 回答
  • 0 關(guān)注
  • 246 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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