I have a list of dictionary that look like that
{'ip_src': '2.2.2.2',
'ip_dst': '1.1.1.1',
'src_id': 43,
'src_name': 'test1',
'dst_id': 48,
'dst_name': 'test2'},
{'ip_src': '1.1.1.1',
'ip_dst': '2.2.2.2',
'src': 48,
'src_name': 'test2',
'dst': 43,
'dst_name': 'test1'},
{'ip_src': '4.4.4.4',
'ip_dst': '3.3.3.3',
'src_id': 41,
'src_name': 'test1',
'dst_id': 47,
'dst_name': 'test2'},
{'ip_src': '3.3.3.3',
'ip_dst': '4.4.4.4',
'src': 47,
'src_name': 'test2',
'dst': 41,
'dst_name': 'test1'},
i want to remove the duplicate data connection because the src and dst maybe diffrent but its on the same cable.
so i want that my list will look like this:
{'ip_src': '2.2.2.2',
'ip_dst': '1.1.1.1',
'src_id': 43,
'src_name': 'test1',
'dst_id': 48,
'dst_name': 'test2'},
{'ip_src': '4.4.4.4',
'ip_dst': '3.3.3.3',
'src_id': 41,
'src_name': 'test1',
'dst_id': 47,
'dst_name': 'test2'},
I try to comper every item in the list and make a reves but it didnt work because after i look into it ,i realised that the oreder of the items in the disctionary must be 100% revese and not only the value.
someone have any idea how to solve my problem?
CodePudding user response:
There are many ways to do it, but simplest one is to convert it to dict
with (src_ip, dest_ip) as keys, by dict
property duplicates will be removed.
l = [{'ip_src': '2.2.2.2',
'ip_dst': '1.1.1.1',
'src_id': 43,
'src_name': 'test1',
'dst_id': 48,
'dst_name': 'test2'},
{'ip_src': '1.1.1.1',
'ip_dst': '2.2.2.2',
'src': 48,
'src_name': 'test2',
'dst': 43,
'dst_name': 'test1'},
{'ip_src': '4.4.4.4',
'ip_dst': '3.3.3.3',
'src_id': 41,
'src_name': 'test1',
'dst_id': 47,
'dst_name': 'test2'},
{'ip_src': '3.3.3.3',
'ip_dst': '4.4.4.4',
'src': 47,
'src_name': 'test2',
'dst': 41,
'dst_name': 'test1'}]
temp_l = {tuple(sorted([i['ip_src'], i['ip_dst']])): i for i in l}
final = list(temp_l.values())
print(final)
# [{'ip_src': '1.1.1.1',
# 'ip_dst': '2.2.2.2',
# 'src': 48,
# 'src_name': 'test2',
# 'dst': 43,
# 'dst_name': 'test1'},
# {'ip_src': '3.3.3.3',
# 'ip_dst': '4.4.4.4',
# 'src': 47,
# 'src_name': 'test2',
# 'dst': 41,
# 'dst_name': 'test1'}]