zoukankan      html  css  js  c++  java
  • 811. 子域名访问计数

     

     

     

    思路:

    1、用空格截cpdomains[i],得到访问次数num和最低级域名IP;
    2、用‘.’截IP得到words,用domain[]统计顶级域名(即words[-1])和最低级域名(即IP);
    3、若len(words) == 3说明要统计二级域名(即words[1]+words[2]);
    4、遍历domain[],用字典my_dict{}统计各域名的访问次数;
    5、cpdomains[]遍历统计完后将字典按规定格式转成list[str]返回。
     1 class Solution(object):
     2     def subdomainVisits(self, cpdomains):
     3         """
     4         :type cpdomains: List[str]
     5         :rtype: List[str]
     6         """
     7         # 返回值
     8         ans = []
     9         # 用字典统计各级域名及其访问次数
    10         my_dict = {}
    11         for i in range(len(cpdomains)):
    12             # 先用空格截取
    13             li = cpdomains[i].split()
    14             # 空格截取后第一部分是访问次数
    15             num = int(li[0])
    16             # 第二部分是最低一级域名
    17             ip = li[1]
    18             # 将最低一级域名用‘.’截取
    19             words = ip.split('.')
    20             # 存放各级有效域名
    21             domain = []
    22             # 统计顶级域名
    23             domain.append(words[-1])
    24             # 统计三级域名
    25             domain.append(ip)
    26             # 若最低是三级域名,则需统计二级域名
    27             if len(words) == 3:
    28                 domain.append(words[1] + "." + words[2])
    29             # 遍历并统计各域名的访问次数
    30             for j in domain:
    31                 if j not in my_dict:
    32                     my_dict[j] = num
    33                 else:
    34                     my_dict[j] += num
    35         # 将字典按规定转成List[str]
    36         for key, value in my_dict.items():
    37             ans.append(str(value) + " " + key)
    38         return ans
    39 
    40 
    41 if __name__ == '__main__':
    42     solution = Solution()
    43     print(solution.subdomainVisits(["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]))
  • 相关阅读:
    os.path.basename()和os.path.splitext()
    关于pytorch中@和*的用处
    Python | os.path.join() method
    SURF (Speeded Up Robust Features,加速稳健特征)
    Canny算子
    医学影像中常见名词解释
    sigmod、tanh、ReLU激活函数的实现
    plt.gca()坐标轴移动
    损失函数和梯度下降解释
    torch和numpy的相互转换
  • 原文地址:https://www.cnblogs.com/panweiwei/p/12800126.html
Copyright © 2011-2022 走看看