我有字典:
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)
发布于 2018-09-08 09:00:31
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)
这样就可以更容易地制作主机-> (url,services)字典,而不是删除重复的“手动”:
data_by_hostname = {get_host(url): (url, services)
for url, services in hostServiceDict.items()}
这个dict负责删除重复的主机名。
然后,如果需要,您可以再次从值构造url ->服务二元化:
result = dict(data_by_hostname.values())
发布于 2018-09-08 09:02:49
您可以通过拥有一个列表并使用已经跟踪的ips验证新ip来跟踪不同的ips --这将需要对您的逻辑进行如下小的更改:
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)
https://stackoverflow.com/questions/52233524
复制相似问题