首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >删除字典中的类似条目

删除字典中的类似条目
EN

Stack Overflow用户
提问于 2018-09-08 08:37:01
回答 2查看 69关注 0票数 0

我有字典:

代码语言:javascript
运行
复制
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是不同的。我需要删除第二个复制的,使它像:

代码语言:javascript
运行
复制
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'],
} 

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

代码语言:javascript
运行
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-08 09:00:31

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

代码语言:javascript
运行
复制
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解析:

代码语言:javascript
运行
复制
import re

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

这样就可以更容易地制作主机-> (url,services)字典,而不是删除重复的“手动”:

代码语言:javascript
运行
复制
data_by_hostname = {get_host(url): (url, services)
                    for url, services in hostServiceDict.items()}

这个dict负责删除重复的主机名。

然后,如果需要,您可以再次从值构造url ->服务二元化:

代码语言:javascript
运行
复制
result = dict(data_by_hostname.values())
票数 2
EN

Stack Overflow用户

发布于 2018-09-08 09:02:49

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

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52233524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档