我的字典是這樣的:計數每唯一鍵唯一值的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