我的字典是这样的:计数每唯一键唯一值的Python字典
yahoo.com|98.136.48.100
yahoo.com|98.136.48.105
yahoo.com|98.136.48.110
yahoo.com|98.136.48.114
yahoo.com|98.136.48.66
yahoo.com|98.136.48.71
yahoo.com|98.136.48.73
yahoo.com|98.136.48.75
yahoo.net|98.136.48.100
g03.msg.vcs0|98.136.48.105
中,我有重复键和值。我想要的是具有唯一键(ips)和唯一值(域)的最终字典。我已经在下面的代码:
for dirpath, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, '*.txt'):
with open(os.path.join(dirpath, filename)) as f:
for line in f:
if line.startswith('.'):
ip = line.split('|',1)[1].strip('\n')
semi_domain = (line.rsplit('|',1)[0]).split('.',1)[1]
d[ip]= semi_domains
if ip not in d:
key = ip
val = [semi_domain]
domains_per_ip[key]= val
但这是行不通的。有人能帮我解决这个问题吗?
为什么你使用'startswith( '')'? – Kasramvd 2014-10-17 10:00:37
你是什么意思*“重复键”*?字典中的键已经是唯一的。 – jonrsharpe 2014-10-17 10:03:18