擅长:python、mysql、java
<p>你在每个循环中重新定义字典。相反,您需要定义一次字典,然后在每个循环中附加到字典。你知道吗</p>
<pre><code>data = {}
for line in lines:
switch_ip = buildings[build_pre]
total = int(line.split(';')[3])
MAX = int(line.split(';')[4])
if switch_ip not in data:
data[buildings[build_pre]] = {
switch_ip: {
'max': MAX,
'total':total
}
}
</code></pre>
<p>这样,您就可以在每次迭代时为字典添加一个键,而不是试图构建一个全新的字典。你知道吗</p>