1 回答
TA贡献1827条经验 获得超7个赞
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))
另一个答案是每个密钥都与其各自的数据配对。我已经更改了您的输入,您可以使用以下代码轻松理解。
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)
添加回答
举报