class Solution(object):
def subdomainVisits(self, cpdomains):
"""
:type cpdomains: List[str]
:rtype: List[str]
"""
ant = collections.Counter()
for domains in cpdomains:
count,domain = domains.split()
count = int(count)
frags = domain.split('.')
for i in range(len(frags)):
ant['.'.join(frags[i:])] += count
return['{} {}'.format(x,y) for y,x in ant.items()]
像“discuss.leetcode.com”这样的网站域包含各种子域。 在顶层,我们有“com”,在下一个层次,我们有“leetcode.com”,最低的层次是“discuss.leetcode.com”。 当我们访问像“discuss.leetcode.com”这样的域名时,我们也会隐式访问父域“leetcode.com”和“com”。
给定输入,计算每个子域的访问次数。
. join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
[ i: ] 表示 包括 i 之后所有的index
In [1]: '{0},{1}'.format('kzc',18)
Out[1]: 'kzc,18'
In [2]: '{},{}'.format('kzc',18)
Out[2]: 'kzc,18'
In [3]: '{1},{0},{1}'.format('kzc',18)
Out[3]: '18,kzc,18'