1 回答

TA貢獻1827條經驗 獲得超8個贊
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))
另一個答案是每個密鑰都與其各自的數(shù)據(jù)配對。我已經更改了您的輸入,您可以使用以下代碼輕松理解。
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)
添加回答
舉報