'''
编写Python脚本,分析xx.log文件,按域名统计访问次数 xx.log文件内容如下:
https://www.sogo.com/ale.html
https://www.qq.com/3asd.html
https://www.sogo.com/teoans.html
https://www.bilibili.com/2
https://www.sogo.com/asd_sa.html
https://y.qq.com/
https://www.bilibili.com/1
https://dig.chouti.com/
https://www.bilibili.com/imd.html
https://www.bilibili.com/ 输出:
4 www.bilibili.com
3 www.sogo.com
1 www.qq.com
1 y.qq.com
1 dig.chouti.com '''

首先我们拿到题目进行需求分析:

1、先获取数据就是域名

获取数据我们可以用正则,或者域名还是有相同点可以用split切分

2、统计域名访问的次数

可以用Python的内置模块来统计,

3、然后就是输出要求的格式

sorted内置函数用来排序

然后开始最轻松的活,开始码字:

#第一种方式
import re
from collections import Counter
with open("xx.log","r",encoding="utf-8") as f:
data=f.read()
res=re.findall(r"https://(.*?)/.*?",data)
dic=Counter(res) ret=sorted(dic.items(),key=lambda x:x[1],reverse=True) for k,v in ret:
print(v,k) #第二种方式
dic={}
with open("xx.log","r",encoding="utf-8") as f:
for line in f:
line=line.split("/")[2]
if line not in dic:
dic[line]=1
else:
dic[line]+=1
ret=sorted(dic.items(),key=lambda x:x[1],reverse=True)
for k,v in ret:
print( v,k)

这道题目考了这些知识点,re模块,匿名函数,内置函数sorted,collections中的Counter

这些在基础篇都找得到相应的博客,

我们就来说说collections中的Counter

我们直接打开源码

Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)

再看源码中的使用方法:

>>> c = Counter('abcdeabcdabcaba') # count elements from a string   生成计数对象

>>> c.most_common(3) # three most common elements   这里的3是找3个最常见的元素
[('a', 5), ('b', 4), ('c', 3)]

>>> c.most_common(4)      这里的4是找4个最常见的元素
[('a', 5), ('b', 4), ('c', 3), ('d', 2)]

>>> sorted(c) # list all unique elements   列出所有独特的元素
['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements())) # list elements with repetitions
'aaaaabbbbcccdde'

这里的elements 不知道是什么?那就继续看源码:

def elements(self):
  '''Iterator over elements repeating each as many times as its count.

迭代器遍历元素,每次重复的次数与计数相同

>>> sum(c.values()) # total of all counts   计数的总和
15

>>> c['a'] # count of letter 'a'    字母“a”的数

5

>>> for elem in 'shazam': # update counts from an iterable  更新可迭代计数在新的可迭代对象
... c[elem] += 1 # by adding 1 to each element's count      在每个元素的计数中增加1
>>> c['a'] # now there are seven 'a'            查看‘a’的计数,加上上面刚统计的2个,总共7个“a”
7
>>> del c['b'] # remove all 'b'      删除所有‘b’的计数
>>> c['b'] # now there are zero 'b'     
0

>>> d = Counter('simsalabim') # make another counter     
>>> c.update(d) # add in the second counter     在第二个计数器中添加

>>> c['a'] # now there are nine 'a'     
9

>>> c.clear() # empty the counter    qingg
>>> c
Counter()

Note: If a count is set to zero or reduced to zero, it will remain
in the counter until the entry is deleted or the counter is cleared:

如果计数被设置为零或减少到零,它将保持不变

在计数器中,直到条目被删除或计数器被清除:

>>> c = Counter('aaabbc')
>>> c['b'] -= 2 # reduce the count of 'b' by two
>>> c.most_common() # 'b' is still in, but its count is zero
[('a', 3), ('c', 1), ('b', 0)]

大约就这几个用法:大家拓展可以自己翻看源码

python3 开发面试题(collections中的Counter)6.7的更多相关文章

  1. python3 开发面试题(面向对象)6.6

    """ 封装.继承.多态 1. 谈谈你对面向对象的理解? 2. Python面向对象中的继承有什么特点? 3. 面向对象深度优先和广度优先是什么? 4. 面向对象中sup ...

  2. python3 开发面试题(创建表结构)6.9

    纯sql语句写出: '''设计 图书管理系统 表结构: - 书 - 书名 - 作者 - 姓名 - 出版社 - 出版社名称 - 地址 一本书只能由一家出版社出版 --> 多对一(书对出版社) 一本 ...

  3. python3 开发面试题(常用模块以及第三方库)6.5

    """ 1. os和sys都是干什么的? 2. 你工作中都用过哪些内置模块? 3. 有没有用过functools模块? """ #sys模块 ...

  4. python3 开发面试题(去重保持原来的顺序)6.2

    """ l1 = [11, 2, 3, 22, 2, 4, 11, 3] 去重并保持原来的顺序 """ #方式一 for 循环方法 l1 = ...

  5. python3 开发面试题(字典和拷贝)5.30

    """ 问:执行完下面的代码后, l,m的内容分别是什么? """ def func(m): for k,v in m.items(): m ...

  6. python3 开发面试题(生成列表)6.1

    话不多说直接上题: 生成如下列表: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12]] # 方式一 list1 ...

  7. python3 开发面试题(装饰器必须考)6.4

    def f(): print("2018-06-04") # 每次调用f的时候 在打印"2018-06-04" 之前打印一句 开始, 之后再打印一句 结束 de ...

  8. python3 开发面试题(%s和format的区别)5.31

    在格式化字符串中有两种方法: 1.%s 2.format 大家常用的是哪一种方法?为什么要用你选的这种方法? 我们先看一个例子: 首先我们定义一个我军需要击杀的恐怖分子的地理坐标为 c=(128,12 ...

  9. php开发面试题---2、php常用面试题二(表单提交方式中的get和post有什么区别)

    php开发面试题---2.php常用面试题二(表单提交方式中的get和post有什么区别) 一.总结 一句话总结: 数据位置:get参数在url里面,post在主体里面 数据大小:get几kb,pos ...

随机推荐

  1. Spring cloud 微服务架构 Eureka篇

    1 服务发现 ## 关于服务发现 在微服务架构中,服务发现(Service Discovery)是关键原则之一.手动配置每个客户端或某种形式的约定是很难做的,并且很脆弱.Spring Cloud提供了 ...

  2. Spark记录-Spark On YARN内存分配(转载)

    Spark On YARN内存分配(转载) 说明 按照Spark应用程序中的driver分布方式不同,Spark on YARN有两种模式: yarn-client模式.yarn-cluster模式. ...

  3. python AjaxSpider 代码演示

    import re # 引入正则表达式 import json # 引入 json import pymongo # 引入mongo数据库 import requests # 引入HTTP请求协议 f ...

  4. java代码实现图片处理功能。对图片质量进行压缩。

    java图片处理有点头疼,找了很多资料.在这里进行一个汇总,记录下个人的体验,也希望对大家有所帮助. 需求:浏览的图片需要在1M一下. 1.真正对图片的质量进行压缩的(不是通过修改图片的高,宽进行缩小 ...

  5. HTML+CSS写下拉菜单

    今天学习了使用HTML+CSS实现下拉菜单效果,在这个例子中,我学到了如下知识点: 设置背景图片(background-image.background-size) 如何让无序列表横向显示(float ...

  6. python3之模块random随机数

    1.random.random() 随机生成一个大于0小于1的随机数. print(random.random()) 0.03064765450719098 2.random.uniform(a,b) ...

  7. RESTful 个人理解总结【转】

    转自:http://www.cnblogs.com/wang-yaz/p/9237981.html 一.什么是RESTful 面向资源 简单的说:RESTful是一种架构的规范与约束.原则,符合这种规 ...

  8. SPListItem.UpdateOverwriteVersion()真的不会创建新版本吗?

    根据msdn文档, SPListItem.UpdateOverwriteVersion(): Updates the item without creating another version of ...

  9. idea如何编译maven项目

    这里我们介绍两种方式,如何调试出窗口 点击菜单栏View->Tool  Windows->Maven projects 点击菜单栏Help->Find Action(Ctrl+Shi ...

  10. tab切换webuploader失效的解决方法

    <script type="text/javascript"> $(document).ready(function () { $('#tt').tabs({ bord ...