<p>请参见下面的一些内联注释</p>
<pre><code>apdb = {'AP Database': [{'AP Type': '110H',
'Name': 'varagu',
'Public IP': '100.20.300.400',
'Wired MAC Address': 'aa:bb:cc:dd:ee:11'},
{'AP Type': '110H',
'Name': 'thinai',
'Public IP': '100.20.300.500',
'Wired MAC Address': 'aa:bb:cc:dd:ee:22'},
{'AP Type': '110H',
'Name': 'Ragi',
'Public IP': '100.20.300.600',
'Wired MAC Address': 'aa:bb:cc:dd:ee:33'}]}
ap_database = apdb.get('AP Database')
# no need to do this since ap_database is already a list, so we can iterate over it.
# apall = ap_database[0], ap_database[1], ap_database[2]
# ip_list does not change, so we can define it once outside the loop
ip_list = ['100.20.300.400', '100.20.300.500', '100.20.300.700']
# let us add two more variables to hold the fail and success list
success_list = []
fail_list = []
for ap in ap_database:
# ap is a dict, so use ap.get('key') or ap['key'] instead of ap.__getitem__('key')
# note that ap.get('key') returns None if key does not exist, while ap['key'] throws an error.
public = ap.get('Public IP')
name = ap.get('Name')
# check whether public is in ip_list and print appropriate message
if public in ip_list:
print public, name, 'Success'
success_list.append(ap)
else:
print public, name, 'Fail'
fail_list.append(ap)
</code></pre>