简体   繁体   English

比较和更新字典中的列表值

[英]Comparing & updating list values inside of dictionaries

dict1 = {
    "domain1": ["53/tcp,open,domain", "80/tcp,open,http"],
    "domain2": ["22/tcp,open,ssh", "25/tcp,open,smtp", "80/tcp,open,http",
                "443/tcp,open,https"],
    "domain3":["22/tcp,open,ssh"]}

I want to compare dict2 with dict1 and check if there are new keys or values (which is list of open ports),If yes then update the dict1 我想将dict2dict1进行比较,并检查是否有新键或值(这是开放端口的列表),如果是,则更新dict1

dict2 = {
    "domain3":["22/tcp,open,ssh","443/tcp,open,https"],
    "domain4":["80/tcp,open,http", "443/tcp,open,https"],
    "domain5":["80/tcp,open,http", "443/tcp,open,https"]}

I did first part of the task ie to find any new keys by comparing dict2 with dict1 keys & checking if there are any new keys in dict2 & updating dict1 . 我做的任务,即第一部分通过比较发现任何新的密钥dict2dict1 及是否有任何新的密钥检查dict2和更新dict1

new_item = {}
for i in dict2.keys():
    if i not in dict1.keys():
        new_item[i] = dict2[i]
        dict1[i] = dict2[i]
print("NEW DOMAINS FOUND : ",new_item)
print(dict1) ## UPDATED with New Domains Found

Here's the output: 这是输出:

NEW DOMAINS FOUND :  {
  'domain4': ['80/tcp,open,http', '443/tcp,open,https'],
  'domain5': ['80/tcp,open,http', '443/tcp,open,https']}
 {'domain1': ['53/tcp,open,domain', '80/tcp,open,http'],
  'domain2': ['22/tcp,open,ssh', '25/tcp,open,smtp', '80/tcp,open,http',
              '443/tcp,open,https'],
  'domain3': ['22/tcp,open,ssh'],
  'domain4': ['80/tcp,open,http', '443/tcp,open,https'],
  'domain5': ['80/tcp,open,http', '443/tcp,open,https']}

I need help solving second part of the task which is compare the values of dict2 with dict1, and if there are any new values in dict2 then update the dict1 with those values. 我需要帮助解决该dict2的值与dict1比较任务的第二部分,如果有任何新的价值dict2然后更新这些值dict1。

If you look at dict2[domain3] and dict1[domain3] , there is an new value in dict2[domain3] , with this, now dict1[domain3] should get updated with those new value. 如果你看一下dict2[domain3]dict1[domain3]有在新的价值dict2[domain3]这一点,现在dict1[domain3]应该得到这些新的值更新。

Output which I want on comparing dict2 with dict1 & updating values/keys: 我想要将dict2dict1比较并更新值/键的输出:

dict1

{'domain1': ['53/tcp,open,domain', '80/tcp,open,http'],
 'domain2': ['22/tcp,open,ssh', '25/tcp,open,smtp', '80/tcp,open,http',
             '443/tcp,open,https'],
 'domain3': ["22/tcp,open,ssh", "443/tcp,open,https"],
 'domain4': ['80/tcp,open,http', '443/tcp,open,https'],
 'domain5': ['80/tcp,open,http', '443/tcp,open,https']}

If you need more information or have doubts, please leave the comment, and I will update the question. 如果您需要更多信息或有疑问,请留下评论,我将更新问题。

If the ordering of the elements inside the lists is not important, you can use the following: 如果列表内元素的顺序不重要,则可以使用以下命令:

dict3 = {}
for k, v in dict2.items():
    dict3[k] = list(set(dict1.get(k, []) + v))

Resulting dict3 : 结果dict3

{'domain3': ['443/tcp,open,https', '22/tcp,open,ssh'], 
 'domain5': ['80/tcp,open,http', '443/tcp,open,https'], 
 'domain4': ['80/tcp,open,http', '443/tcp,open,https']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM