在python字典中计算每个唯一键的唯一值

时间:2014-10-17 09:57:55

标签: python loops dictionary unique counting

我有这样的字典:

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

但这不能正常工作。有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

您可以使用zip功能分隔列表中的ipsdomains,然后使用set获取唯一条目!

>>>f=open('words.txt','r').readlines()
>>> zip(*[i.split('|') for i in f])
[('yahoo.com', 'yahoo.com', 'yahoo.com', 'yahoo.com', 'yahoo.com', 'yahoo.com', 'yahoo.com', 'yahoo.com', 'yahoo.net', 'g03.msg.vcs0'), ('98.136.48.100\n', '98.136.48.105\n', '98.136.48.110\n', '98.136.48.114\n', '98.136.48.66\n', '98.136.48.71\n', '98.136.48.73\n', '98.136.48.75\n', '98.136.48.100\n', '98.136.48.105')]
>>> [set(dom) for dom in zip(*[i.split('|') for i in f])]
[set(['yahoo.com', 'g03.msg.vcs0', 'yahoo.net']), set(['98.136.48.71\n', '98.136.48.105\n', '98.136.48.100\n', '98.136.48.105', '98.136.48.114\n', '98.136.48.110\n', '98.136.48.73\n', '98.136.48.66\n', '98.136.48.75\n'])]

然后使用len,您可以找到唯一对象的数量! 与列表理解相关的所有内容

>>> [len(i) for i in [set(dom) for dom in zip(*[i.split('|') for i in f])]]
[3, 9]

答案 1 :(得分:0)

使用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)))