擅长:python、mysql、java
<p>使用字典而不是数组:</p>
<pre><code>corrections = {}
f2 = open('file2.txt', 'r')
for line2 in f2:
(key, value) = line2.split(' ')
corrections[key] = value
f1 = open('file1.txt', 'r')
for line1 in f1:
(key, value) = line1.split(' ')
if key in corrections:
print(key, corrections[key])
else:
print(line1)
</code></pre>
<p>以下是您的字典在读取更正文件后的外观:</p>
^{pr2}$
<p>当<code>file1</code>被读取时,行也被拆分,只是为了检查第一个值是否是字典中的键之一(<code>key in corrections</code>)。如果不是,则只打印原始行(即使它包含<code>error</code>)。但是如果我们有一个更正,它会被打印出来(<code>print (key, corrections[key]</code>)。这里我们使用<code>print</code>在其参数之间插入一个空格。在</p>