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

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

在 python 中的另一個(gè)文件的行中查找模式

在 python 中的另一個(gè)文件的行中查找模式

慕運(yùn)維8079593 2023-10-11 16:08:16
我正在學(xué)習(xí) python,所以我有兩個(gè)包含很多行的文件:文件171528145214587文件2country_hoj_17458   9  CA   5    CA 78.4country_hoj_1452   10  CA   15   CA 96.5country_hoj_14787  19  CA   51   CA 12.4country_hoj_15742  19  CA   51   CA 12.4country_hoj_171528  19  CA   51   CA 12.4我想打印第一列中模式(數(shù)字)文件 1 與文件 2 匹配的行。我想要一個(gè)這樣的輸出文件 country_hoj_1452   10  CA   15   CA 96.5 country_hoj_14787  19  CA   51   CA 12.4我的腳本是這樣的:filename = numbers.txtfilename2 = data.txtwith open(filename) as f:    with open (filename2) as m:        for line in f:                if line in m:                       print (m)     我需要修復(fù)什么?有人可以幫助我并支持我嗎?多謝
查看完整描述

1 回答

?
慕仙森

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

filename = 'numbers.txt'

filename2 = 'data.txt'


with open(filename) as numberLines:

    with open (filename2) as dataLines:

        nL = numberLines.read().splitlines()

        dL = dataLines.read().splitlines()

        dataReadLines = [j for i in nL for j in dL if i in j]

        #dataReadLines = [i for i in nL]

        print (str(dataReadLines))

另一個(gè)答案是每個(gè)密鑰都與其各自的數(shù)據(jù)配對(duì)。我已經(jīng)更改了您的輸入,您可以使用以下代碼輕松理解。


from collections import defaultdict


filename = 'numbers.txt'

filename2 = 'data.txt'


with open(filename) as numberLines:

    with open (filename2) as dataLines:

        nL = numberLines.read().splitlines()

        dL = dataLines.read().splitlines()

        defDList = defaultdict(list)

        dataReadLines = [defDList[i].append(j) for i in nL for j in dL if i in j]

        #dataReadLines = [i for i in nL]

        print (defDList)


查看完整回答
反對(duì) 回復(fù) 2023-10-11
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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