【问题标题】:Adding lines of a text file to a dictionary将文本文件的行添加到字典
【发布时间】:2017-09-27 12:43:20
【问题描述】:

我一直在努力思考我将如何做到这一点,但我似乎无处可去。

如果我有一个文本文件,其中包含一个主机名及其对应的 IP 地址:

The result of www.espn.com is 199.181.133.15
The result of www.espn.com is 199.454.152.10
The result of www.espn.com is 20.254.215.14
The result of www.google.com is 141.254.15.14
The result of www.google.com is 172.14.54.153
The result of www.yahoo.com is 181.145.254.12

如何在列表或字典中获取地址及其对应的 IP 地址?

所以www.google.com 会是这样的:

("www.google.com", 141.254.15.14, 172.14.54.153)

上面的行将始终采用相同的格式,因此我可以遍历文件,获取上面的内容,使用split(),并将地址添加到字典中。

  ....... 
        ....
            dictA = {}
            for line in f:
                splitLine = line.split()
                    dictA = {splitLine[2]: splitLine[3]}

键将只是网站,值将是其所有对应的 IP 地址。我只需要将它们放在列表或其他东西中即可。

【问题讨论】:

    标签: python list dictionary


    【解决方案1】:

    您可以使用collections 中的defaultdict 并将默认设置为列表:

    >>> from collections import defaultdict
    >>> s = '''The result of www.espn.com is 199.181.133.15
    ... The result of www.espn.com is 199.454.152.10
    ... The result of www.espn.com is 20.254.215.14
    ... The result of www.google.com is 141.254.15.14
    ... The result of www.google.com is 172.14.54.153
    ... The result of www.yahoo.com is 181.145.254.12'''.splitlines()
    >>> dictA = defaultdict(list)
    >>> for line in s:
    ...     words = line.split()
    ...     dictA[words[3]].append(words[-1])
    ...
    >>> dictA
    defaultdict(<type 'list'>, {'www.yahoo.com': ['181.145.254.12'], 'www.espn.com': ['199.181.133.15', '199.454.152.10', '20.254.215.14'], 'www.google.com': ['141.254.15.14', '172.14.54.153']})
    
    >>> for key, val in dictA.items():
    ...     print key, val
    ...
    www.yahoo.com ['181.145.254.12']
    www.espn.com ['199.181.133.15', '199.454.152.10', '20.254.215.14']
    www.google.com ['141.254.15.14', '172.14.54.153']
    

    【讨论】:

      【解决方案2】:

      使用列表字典。对于简单的实现,请使用defaultdict,如下所示:

      from collections import defaultdict
      dictA = defaultdict(list)
      for line in f:
          splitLine = line.split()
          dictA[splitLine[3]].append(splitLine[5])
      

      【讨论】:

        【解决方案3】:

        正如其他人所说,使用defaultdict 将您的 键的值作为列表很容易,只需将 IP 地址附加到该列表即可。

        from collections import defaultdict
        
        dictA = defaultdict(list)
        with open('filename', 'r') as f:
            #Where domain is the 4th item in the line, and ip is the 6th
            for domain, ip in ((line[3], line[5]) for line in map(str.split, f.readlines())):
                dictA[domain].append(ip)
        
        print dictA
        

        defaultdict(, {'www.yahoo.com': ['181.145.254.12'], 'www.espn.com': ['199.181.133.15', '199.454.152.10', '20.254.215.14'] , 'www.google.com': ['141.254.15.14', '172.14.54.153']})

        您可以通过将每行推入str.split 来缩短行数并且仍然有意义。如果您的文件很大,您可以从itertools 改用imap(语法相同)以节省内存。

        【讨论】:

          【解决方案4】:

          使用字典,您可以这样做:

          domain_name_to_ip_mappping = {}
          with open('filename') as f:
              for line in f:
          
                  data = line.split()
                  domain_name = data[3]
                  ip = data[-1]
                  if domain_name in domain_name_to_ip_mappping:
                      #domain name already exists, so simply append ip
                      domain_name_to_ip_mappping[domain_name].append(ip) 
                  else:  
                      #create a domain entry and init a list with current ip 
                      domain_name_to_ip_mappping[domain_name] = [ip] 
          

          【讨论】:

            猜你喜欢
            • 2015-06-24
            • 1970-01-01
            • 1970-01-01
            • 2015-03-21
            • 1970-01-01
            • 2015-08-26
            • 1970-01-01
            • 2022-12-06
            • 2021-12-30
            相关资源
            最近更新 更多