【问题标题】:Count unique values per unique keys in python dictionary计算python字典中每个唯一键的唯一值
【发布时间】:2023-04-06 20:36:01
【问题描述】:

我有这样的字典:

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('.')
  • 你是什么意思“重复键”?字典中的键已经是唯一的。

标签: python loops dictionary unique counting


【解决方案1】:

使用默认字典:

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】:

    您可以使用zip 函数将ipsdomains 分开在tow 列表中,然后使用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]
    

    【讨论】:

      猜你喜欢
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多