如何比较字典,看看有什么变化? [英] How to compare dictionaries and see what changed?
问题描述
-
self.new_port_dict = {}
#存储新端口的字典
从curr_host -
self.old_port_dict = {}
#字典到存储旧的端口from old_host -
self.results_ports_dict = {}
#保存更改/新增端口的结果
I am having 3 dictionaries in my python code :
脚本需要比较哪些端口更改,我几乎在那里只是无法提供帮助我:
self.new_port_dict = {}
# Dictionary to store the new ports from curr_hostself.old_port_dict = {}
# Dictionary to store the old ports from old_hostself.results_ports_dict = {}
# Holds the result of changed/newly added ports
The script needs to compare what port changed, I am almost there just unable to present help me out :
这样输出为:
This gives a output as :
你可以清楚地看到,我也有结果改变的字典。我想打印:
The following Host/ports were available in old scan : !!
{'172.16.0.41': set([(80, 'tcp'), (666, 'tcp')]), '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])}
--------------------------------------------------------
The following Host/ports have been added in new scan: !!
{'172.16.0.41': set([(80, 'tcp'), (22, 'tcp')]), '172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])}
Result Change: for 172.16.0.41 -> set([(22, 'tcp')]) From set([(80, 'tcp'), (666, 'tcp')])
Result Change: for 172.16.0.163 -> set([]) From set([(80, 'tcp'), (22, 'tcp')])
As you can clearly see , I have the resulting changed dictionary as well. I want to just print :
推荐答案
我相信你没有比较字典,但实际上是与密钥相对应的值。
这里的基本想法是:
-
主持人不得总是存在于过去和现在的扫描中,避免使用
collections.defaultdict
,以确保即使在没有主机的情况下也可以直接比较值。因为缺少键的值将被自动生成(空的集
)
The basic ideas here are:
在设置
之间有3个操作
Have 3 operations on the set
of ports
old - new
:查看哪些端口在旧扫描中,但不再在新的(已删除端口)中
old - new
: to see which ports were in the old scan but no longer in the new (deleted ports)
new - old
:查看哪些端口在新扫描中,但不在旧(添加端口)中
new - old
: to see which ports are in the new scan but were not in the old (added ports)
有几种方法可以将代码移动到优化中,但我想这点更清晰
There are several ways to move the code around to optimize, but I guess the point is much more clarity.
希望它有帮助
import collections
scan0 = collections.defaultdict(set, {
'172.16.0.41': set([(80, 'tcp'), (666, 'tcp')]),
'172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])
})
scan1 = collections.defaultdict(set, {
'172.16.0.41': set([(80, 'tcp'), (22, 'tcp')]),
'172.16.0.163': set([(80, 'tcp'), (22, 'tcp')])
})
hosts = sorted(set(scan0.keys() + scan1.keys()))
scan_same = dict()
scan_new = dict()
scan_del = dict()
for host in hosts:
scan_same[host] = scan0[host] & scan1[host]
scan_new[host] = scan1[host] - scan0[host]
scan_del[host] = scan0[host] - scan1[host]
print()
print('-' * 10, 'Same')
for host, ports in scan_same.items():
print(host, ':')
for port in ports:
print(':::', port[0], '/', port[1])
print()
print('*' * 10, 'Added')
for host, ports in scan_new.items():
print(host, ':')
for port in ports:
print(':::', port[0], '/', port[1])
print()
print('=' * 10, 'Deleted')
for host, ports in scan_del.items():
print(host, ':')
for port in ports:
print(':::', port[0], '/', port[1])
这将输出:
---------- Same
172.16.0.163 :
::: 80 / tcp
::: 22 / tcp
172.16.0.41 :
::: 80 / tcp
*********** Added
172.16.0.163 :
172.16.0.41 :
::: 22 / tcp
========== Deleted
172.16.0.163 :
172.16.0.41 :
::: 666 / tcp
这篇关于如何比较字典,看看有什么变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!