删除字典中的类似条目

2024-04-27 21:59:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有字典:

hostServiceDict = {"http://192.168.1.1:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO'],
"http://192.168.1.2:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'UDDC'],
"http://192.168.1.3:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'HTTPServer'],
"http://192.168.1.4:8080/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset'],
"http://192.168.1.5:8080/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'WCS', 'NCSS'],
"http://192.168.1.5:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'WCS', 'NCSS'],
"http://192.168.1.6:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'DAP4'],
"http://192.168.1.7:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NCML', 'DAP4'],
"http://192.168.1.8:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset'],
"http://192.168.1.9:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'UDDC'],
"http://192.168.1.18:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset'],
"http://192.168.1.18:8800/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset']
}

两个条目有一些ip地址,但是端口部分对于http://192.168.1.5 and http://192.168.1.18是不同的。我需要删除第二个副本,使之像:

hostServiceDict = {"http://192.168.1.1:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO'],
"http://192.168.1.2:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'UDDC'],
"http://192.168.1.3:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'HTTPServer'],
"http://192.168.1.4:8080/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset'],
"http://192.168.1.5:8080/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'WCS', 'NCSS'],
"http://192.168.1.6:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'DAP4'],
"http://192.168.1.7:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NCML', 'DAP4'],
"http://192.168.1.8:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset'],
"http://192.168.1.9:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'UDDC'],
"http://192.168.1.18:80/thredds/catalog.xml": ['OPENDAP', 'WMS', 'HTTP', 'ISO', 'NetcdfSubset'],
} 

我已经试过了,但它仍然给了我和原点一样的结果:

result = {}
for urls, services in hostServiceDict.items():
    i = urls.strip('http://').strip('thredds/catalog.xml').split(':')
    ip = i[0]
    if ip not in result.items():
        if ip in urls:
            result[urls] = services

print(result)

Tags: iphttpisoxmlresulturlscatalogthredds
2条回答

您可以通过拥有一个列表并使用已跟踪的ip验证新ip来跟踪不同的ip这将需要对您的逻辑进行如下小的更改:

result = {}
distinct_ips = []
for urls, services in hostServiceDict.items():
    i = urls.strip('http://').strip('thredds/catalog.xml').split(':')
    ip = i[0]
    if ip not in distinct_ips:
        distinct_ips.append(ip)
        if ip in urls:
            result[urls] = services

print(result)

if ip not in result.items():永远找不到ip,因为IP不在results中。 你必须跟踪你看到的IP:

result = {}
seen_ips = set()
for url, services in hostServiceDict.items():
    ip = url.strip('http://').strip('thredds/catalog.xml').split(':')[0]
    if ip not in seen_ips:
        seen_ips.add(ip)
        result[url] = services

print(result)

为了使代码更好,您可以进行真正的url解析:

import re

def get_host(url):
    return re.match(r'https?://([^:/]+).*', url).groups(0)

这样就更容易创建一个主机字典,而不是“手动”删除重复项:

data_by_hostname = {get_host(url): (url, services)
                    for url, services in hostServiceDict.items()}

这个dict负责删除重复的主机名。你知道吗

然后,如果需要,您可以根据以下值再次构造url->;services词汇表:

result = dict(data_by_hostname.values())

相关问题 更多 >