Count unique values per unique keys in python dictionary

早过忘川 提交于 2020-01-25 11:38:05

问题


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?


回答1:


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



回答2:


you can use zip function to separate the ips and domains in tow list , then use set to get the unique entries !

>>>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'])]

and then with len you can find the number of unique objects ! all in one line with list comprehension :

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


来源:https://stackoverflow.com/questions/26422507/count-unique-values-per-unique-keys-in-python-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!