python中字典的键是唯一的吗,在python字典中为每个唯一键计算唯一值

I have dictionary like this:

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

in which I have repetitive keys and values. And what I want is a final dictionary with unique keys (ips) and count of unique values (domains). I have laready below code:

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

but this is not working properly. Can somebody help me out with this?

解决方案

Use a defaultdict:

from collections import defaultdict

d = defaultdict(set)

with open('somefile.txt') as thefile:

for line in the_file:

if line.strip():

value, key = line.split('|')

d[key].add(value)

for k,v in d.iteritems(): # use d.items() in Python3

print('{} - {}'.format(k, len(v)))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值