简体   繁体   English

有没有一种方法可以使用键的值比较两个字典?

[英]Is there a way of comparing two dictionaries using the values of the keys?

I am working on two dictionaries. 我正在写两个字典。 The first dictionary "Blacklist" contains addresses as key and dates as values. 第一个字典“黑名单”包含地址作为键,而日期则作为值。 The second dictionary "transferred" also contains addresses as key and dates as values. 第二个词典“已转移”还包含地址(作为关键字)和日期(作为值)。 I want to compare the two dictionaries to check if any of the "transferred" address is in the "blacklist" address. 我想比较两个字典,以检查是否有任何“已转移”地址在“黑名单”地址中。 if there is a match, i want to check if the date on the blacklist was before or after the transferred date. 如果有匹配项,我想检查黑名单上的日期是在转移日期之前还是之后。 I want it to create a new dictionary "update" with address as key and value to be "before" or "after" as the case may be. 我希望它创建一个新字典“ update”,其中地址作为键,值视情况而定为“ before”或“ after”。

Please take note "94.142.136.0/21" matches "94.142.136.190" 请注意“ 94.142.136.0/21”与“ 94.142.136.190”匹配

blacklist = {'93.118.36.235': '25/02/2016', '62.149.128.160': '05/06/2017', '62.149.128.163': '05/06/2017', '62.149.128.166': '05/06/2017', '62.149.128.72': '05/06/2017', '62.149.128.74': '05/06/2017', '69.163.171.33': '10/03/2014', '69.163.200.61': '22/12/2014', '94.142.136.190': '19/02/2016'}

transferred ={'94.142.136.0/21': '28/06/2019', '185.2.4.0/22': '01/07/2019', '213.158.64.0/19': '01/07/2019', '5.154.240.0/24': '01/07/2019', '78.159.140.0/22': '01/07/2019', '81.88.48.0/20': '01/07/2019'}

found = {}

for k,v in blacklist.items():
     for k1,v1 in transferred.items():
         if k1 = k:
            if v1 > v:
               found.append(k1, 'before')
            else:
               found.append(k1, 'after')

I expect this as the result 我希望这是结果

found = {'94.142.136.0/21': 'before'}

First, you need to check if the IP address in the blacklist dictionary is in one of the CIDR blocks in the transferred dictionary. 首先,您需要检查blacklist字典中的IP地址是否在transferred字典中的CIDR块之一中。 Then you need to convert those date strings to datetime objects so you can compare them 然后,您需要将这些日期字符串转换为datetime对象,以便可以对其进行比较

from ipaddress import ip_address, ip_network
from datetime import date

blacklist = {'93.118.36.235': '25/02/2016', '62.149.128.160': '05/06/2017', '62.149.128.163': '05/06/2017', '62.149.128.166': '05/06/2017', '62.149.128.72': '05/06/2017', '62.149.128.74': '05/06/2017', '69.163.171.33': '10/03/2014', '69.163.200.61': '22/12/2014', '94.142.136.190': '19/02/2016'}

transferred ={'94.142.136.0/21': '28/06/2019', '185.2.4.0/22': '01/07/2019', '213.158.64.0/19': '01/07/2019', '5.154.240.0/24': '01/07/2019', '78.159.140.0/22': '01/07/2019', '81.88.48.0/20': '01/07/2019'}

found = {}

for ip_addr in blacklist:
    for cidr_block in transferred:
        if ip_address(ip_addr) in ip_network(cidr_block):
            blacklist_date = date(*map(int,blacklist[ip_addr].split("/")[::-1]))
            transferred_date = date(*map(int,transferred[cidr_block].split("/")[::-1]))

            if transferred_date > blacklist_date:
                found[cidr_block] =  'before'
            else:
                found[cidr_block] =  'after'

print(found)

Use these methods to convert the formats, check if they are equal. 使用这些方法转换格式,检查它们是否相等。

import socket,struct

def makeMask(n):
    "return a mask of n bits as a long integer"
    return (2L<<n-1) - 1

def dottedQuadToNum(ip):#Pass just 94.142.136.190
    "convert decimal dotted quad string to long integer"
    return struct.unpack('L',socket.inet_aton(ip))[0]

def networkMask(ip,bits):#Pass 94.142.136.0 and 21
    "Convert a network address to a long integer" 
    return dottedQuadToNum(ip) & makeMask(bits)

the ipaddress module seems to do what you want: [] https://docs.python.org/3/library/ipaddress.html ipaddress模块​​似乎可以满足您的要求:[] https://docs.python.org/3/library/ipaddress.html

from ipaddress import ip_network, ip_address

found = {}
for k,v in blacklist.items():
    for k1,v1 in transferred.items():
        if ip_address(k) in ip_network(k1):
            found[k1] = 'before' if v1 > v else 'after'

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

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