Я использовал некоторые функции, которые вы разместили здесь в качестве ответа, и я смог решить проблему!
Спасибо.
Вот закомментированное решение.
Функция
def search_diffences(list_one, list_two):
# Store the result
list_final = []
# Sort by IP
list_one.sort( key = lambda d : d['ip'] )
list_two.sort( key = lambda d : d['ip'] )
# Find the bigger list
bigger_list = list_one
smaller_list = list_two
if len(list_two) > len(list_one):
bigger_list = list_two
smaller_list = list_one
# Start the for inside for
for lo in bigger_list:
found = False # Store if the result was found
pop_i = 0 # Control what dict will be remove in the smaller_list (For code optimization)
for lt in smaller_list:
print("lo['ip']({0}) == lt['ip']({1}): {2}".format(lo['ip'], lt['ip'], lo['ip'] == lt['ip'])) # For debug
if lo['ip'] == lt['ip']: # If the biggest_list as lo ['ip'] was found in smaller_list
found = True # Set found as True
# Store scan_id because will be used more than one time
scan_id_lo = lo['scan_id']
scan_id_lt = lt['scan_id']
# Create a temp list for add in list_final
ip = lo['ip']
# Struct how i want the result
hostname = lo['hostname']
if lo['hostname'] != lt['hostname']:
hostname = '({SCAN_ID_LO}:{VALUE_LO}) != ({SCAN_ID_LT}:{VALUE_LT})'.format(
SCAN_ID_LO=scan_id_lo,
SCAN_ID_LT=scan_id_lt,
VALUE_LO=lo['hostname'],
VALUE_LT=lt['hostname']
)
state = lo['state']
if lo['state'] != lt['state']:
state = '({SCAN_ID_LO}:{VALUE_LO}) != ({SCAN_ID_LT}:{VALUE_LT})'.format(
SCAN_ID_LO=scan_id_lo,
SCAN_ID_LT=scan_id_lt,
VALUE_LO=lo['state'],
VALUE_LT=lt['state']
)
# Create the temp_list
temp_list = {
'ip': ip,
'hostname': hostname,
'state': state
}
# Append temp_list in list_final
list_final.append(temp_list)
# Pop the value because so, the next for of bigger_list does not go through the first value of the smaller list again
smaller_list.pop(pop_i)
# Go to Next value of bigger_list
break
# pop_i++ because if the smaller list does not hit == in the first attempt, then it pops in pop_i value
pop_i += 1
print(found) # Debug
if not found: # If the value of bigger list doesnt exist in smaller_list, append to list_final anyway
scan_id_lo = lo['scan_id']
scan_id_lt = lt['scan_id']
ip = lo['ip']
print("This was not found, adding to list_final", ip)
hostname = '({SCAN_ID_LO}:{VALUE_LO}) != ({SCAN_ID_LT}:{VALUE_LT})'.format(
SCAN_ID_LO=scan_id_lo,
SCAN_ID_LT=scan_id_lt,
VALUE_LO='NOT EXIST',
VALUE_LT=lo['hostname']
)
state = '({SCAN_ID_LO}:{VALUE_LO}) != ({SCAN_ID_LT}:{VALUE_LT})'.format(
SCAN_ID_LO=scan_id_lo,
SCAN_ID_LT=scan_id_lt,
VALUE_LO='NOT EXIST',
VALUE_LT=lo['state']
)
temp_list = {
'ip': ip,
'hostname': hostname,
'state': state
}
list_final.append(temp_list)
# bigger_list.pop(0)
# If smaller_list still have elements
for lt in smaller_list:
scan_id_lt = lt['scan_id']
ip = lt['ip']
hostname = '({SCAN_ID_LO}:{VALUE_LO}) != ({SCAN_ID_LT}:{VALUE_LT})'.format(
SCAN_ID_LO='NOT EXIST',
SCAN_ID_LT=scan_id_lt,
VALUE_LO='NOT EXIST',
VALUE_LT=lt['hostname']
)
state = '({SCAN_ID_LO}:{VALUE_LO}) != ({SCAN_ID_LT}:{VALUE_LT})'.format(
SCAN_ID_LO='NOT EXIST',
SCAN_ID_LT=scan_id_lt,
VALUE_LO='NOT EXIST',
VALUE_LT=lt['state']
)
temp_list = {
'ip': ip,
'hostname': hostname,
'state': state
}
list_final.append(temp_list) # Simple, append
return list_final
Основной код и списки
# First List
list_one = [
{
'ip': '127.0.0.1',
'hostname': 'abc',
'state': 'open',
'scan_id': '2'
},
{
'ip': '127.0.0.2',
'hostname': 'bca',
'state': 'closed',
'scan_id': '2'
},
{
'ip': '100.0.0.4',
'hostname': 'ddd',
'state': 'closed',
'scan_id': '2'
},
{
'ip': '100.0.0.1',
'hostname': 'ggg',
'state': 'up',
'scan_id': '2'
},
]
# Second List
list_two = [
{
'ip': '127.0.0.1',
'hostname': 'abc',
'state': 'closed',
'scan_id': '3'
},
{
'ip': '127.0.0.3',
'hostname': 'qwe',
'state': 'open',
'scan_id': '3'
},
{
'ip': '127.0.0.2',
'hostname': 'xxx',
'state': 'up',
'scan_id': '3'
},
{
'ip': '10.0.0.1',
'hostname': 'ddd',
'state': 'open',
'scan_id': '3'
},
{
'ip': '100.0.0.1',
'hostname': 'ggg',
'state': 'down',
'scan_id': '3'
},
]
print(search_diffences(list_one, list_two))
В результате
[{
'ip': '10.0.0.1',
'hostname': '(3:NOT EXIST) != (2:ddd)',
'state': '(3:NOT EXIST) != (2:open)'
}, {
'ip': '100.0.0.1',
'hostname': 'ggg',
'state': '(3:down) != (2:up)'
}, {
'ip': '127.0.0.1',
'hostname': 'abc',
'state': '(3:closed) != (2:open)'
}, {
'ip': '127.0.0.2',
'hostname': '(3:xxx) != (2:bca)',
'state': '(3:up) != (2:closed)'
}, {
'ip': '127.0.0.3',
'hostname': '(3:NOT EXIST) != (2:qwe)',
'state': '(3:NOT EXIST) != (2:open)'
}, {
'ip': '100.0.0.4',
'hostname': '(NOT EXIST:NOT EXIST) != (2:ddd)',
'state': '(NOT EXIST:NOT EXIST) != (2:closed)'
}]