擅长:python、mysql、java
<p><a href="https://docs.python.org/3.8/tutorial/datastructures.html#more-on-lists" rel="nofollow noreferrer">^{<cd1>}</a>是类型<code>list</code>的方法,而不是<code>dict</code>。始终确保查看完整的方法签名,以查看方法属于什么类型</p>
<p>相反,我们可以使用<a href="https://docs.python.org/3/library/stdtypes.html#dict.update" rel="nofollow noreferrer">^{<cd4>}</a>:</p>
<blockquote>
<p>Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.</p>
<p>update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).</p>
</blockquote>
<p>并在代码中使用此方法,如下所示:</p>
<pre><code>jsonData[key1].update(PersonBUpdate)
jsonData[key2].update(PersonCUpdate)
</code></pre>
<p>这给出了预期的结果:</p>
<pre><code>{'PersonA': {'Age': '35', 'Place': 'Berlin', 'cars': ['Ford', 'BMW', 'Fiat']}, 'PersonB': {'Age': '45', 'Cars': ['Kia', 'Ford'], 'Place': 'Rome'}, 'PersonC': {'Age': '55', 'Place': 'London', 'cars': ['Hyundai', 'Ford']}}
</code></pre>