我有一個字典的串列,看起來像這樣的
{'ip_src'/span>: '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'}。
我想洗掉重復的資料連接,因為src和dst可能是不同的,但它在同一條電纜上。
所以我想讓我的串列看起來像這樣:
我想讓我的串列看起來像這樣。
{'ip_src'/span>: '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'}。
我試著對串列中的每一個專案進行比較,并做一個reves,但是沒有成功,因為在我研究之后,我意識到disctionary中的專案的reder必須是100%的reves,而不是只有值。
有人知道如何解決我的問題嗎?
uj5u.com熱心網友回復:
有很多方法,但最簡單的方法是將其轉換為以(src_ip, dest_ip)為鍵的dict,通過dict屬性,重復的將被洗掉。
l = [{'ip_src'/span>: '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',/span>
# 'ip_dst': '2.2.2.2',/span>
# 'src': 48,
# 'src_name': 'test2',/span>
# 'dst': 43,
# 'dst_name': 'test1'},
# {'ip_src': '3.3.3.3',
# 'ip_dst': '4.4.4.4',
# 'src': 47,
# 'src_name': 'test2',/span>
# 'dst': 41,
# 'dst_name': 'test1'}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311656.html
標籤:
