擅长:python、mysql、java
<p>你的代码有一些问题。在</p>
<ol>
<li><p>当您执行<code>if value not in data_previous:</code>时,您实际上要检查<code>value</code>是否在<code>data_previous</code>的<strong>键中,而不是在其值中。</p></li>
<li><p>当你做<code>zip(current_data_file, pre_data_file)</code>时,你实际上是在看两个字典的对应的<em>对。这里有3个字典,每个字典中有2个键,这就是为什么有6个输出行而不是3个输出行。换句话说,您是成对查找数据,而不是将数据中的每个字典与其他数据中的所有字典进行比较。</p></li>
</ol>
<p>下面是一个示例代码:</p>
<pre><code>def compare_files():
with open('new.json', 'r') as current_data_file, open('old.json','r') as pre_data_file:
# load both data
data_currents = [json.loads(line) for line in current_data_file]
data_previous = [json.loads(line) for line in pre_data_file]
# store the previous names for convenient lookup
pre_names = set([data["name"] for data in data_previous])
# loop through all current data for matching names
for data in data_currents:
print("Match" if data["name"] in pre_names else "No Match")
</code></pre>