python -- 题目不看别人的自己写然后比较
题目一
'''
编写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/
输出:
www.bilibili.com
www.sogo.com
www.qq.com
y.qq.com
dig.chouti.com
'''
import re
domain_dict = {}
with open('./visit.log','r') as fr:
for line in fr.readlines():
pattern = re.compile(r'(http.*?com).*')
domain = pattern.match(line).group(1)
if domain in domain_dict:
domain_dict[domain] = domain_dict[domain]+1
else:
domain_dict[domain] = 1
print(domain_dict)
sorted(domain_dict.items(),key=lambda domain_dict:domain_dict[1],reverse=True)
改进版,优化内存
import re
def buffered_read(file_opened,block_size=4096):
while True:
data = file_opened.read(block_size)
if not data:
break
yield data
domain_dict = {}
with open('./visit.log') as f:
for block in buffered_read(f):
pattern = re.compile(r'https:.*?com')
domain_list = pattern.findall(block)
#domain_dict = [{domain:1} for domain in domain_list]
for key in domain_list:
if key in domain_dict:
domain_dict[key] = domain_dict[key]+1
else:
domain_dict[key] = 1
sorted(domain_dict.items(),key=lambda d:d[1],reverse=True)
# 别人家的方法
#第一种方式
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)
python -- 题目不看别人的自己写然后比较的更多相关文章
- 自学笔记系列:《Python学习手册 第五版》 -写在开始之前
今年双十一,在当当网上买了这本书,很厚很厚的一本书,大概有将近1700页左右,的确是一个“大工程”, 关于这本书的学习,我想采用一种博客的方式进行,既是写给自己,也想分享给每一个对Python学习感兴 ...
- 看别人的代码学习的css
<ul class='y1'> <li><a href="#">菜单</a></li> <li ...
- 看源码和写demo是一种比较容易提升的方式
github就是要这么用才行.看别人的源码,就能了解到很多规范,而写demo,就是自己写出自己的代码.莫欺少年穷
- Python初学者必看(1)
python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...
- php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort)
php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort) 一.总结 核心是排序 ...
- 之前同事问到的一道python题目
Python面试题 之前同事问了一道Python题目如下,暂时归类为面试题 题目:把类似'123.456'的字符串转换成浮点型数据 方法一: >>> print '{:.3f}'.f ...
- 看了xici有写给孩子的信,maybe我也要写给孩子一些东西了
看了xici有写给孩子的信,maybe我也要写给孩子一些东西了
- 孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5
孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- 孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4
孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
随机推荐
- Ubuntu设置静态IP的方法
通过修改配置文件/etc/network/interfaces,如果/etc/resolv.conf中提示nameserver会被resolvconf修改,是临时文件,那么dns server也可以在 ...
- Docker镜像加速设置
地址:https://www.daocloud.io/mirror#accelerator-doc 配置 Docker 加速器 Linux MacOS Windows curl -sSL https: ...
- [读书笔记]Linux命令行与shell编程读书笔记03 文件系统等
1. 文件系统的种类 ext ext2 ext3 ext4 JFS XFS 其中ext3 开始支持journal日志模式 与raid卡类似 有 数据模式 排序模式 以及回写模式 数据模式最安全 回写 ...
- [转帖]召冠总的 Oracle常用的性能诊断语句. --保存学习备查
Copyfrom https://www.cnblogs.com/zhaoguan_wang --1.阻塞及等待事件信息查询-- 查询所有会话的状态.等待类型及当前正在执行的SQL脚本select t ...
- msyql sql语句收集
在不断的学习中,发现了一些新的slq语句,总结于此. 一.复制数据表的sql 1)create table tableName as select * from tableName2 ...
- 反编译微信小程序
最近看了个微信小程序古诗词全集,想知道他的前后端是怎么实现的,所以就想到了反编译.小程序安装后会有个wxapkg格式的文件存在/data/data/com.tencent.mm/MicroMsg/** ...
- 下载 Internet Explorer 11(脱机安装程序)
https://support.microsoft.com/zh-cn/help/18520/download-internet-explorer-11-offline-installer 语言 本 ...
- js運算符
運算符算術運算符.邏輯運算符.賦值運算符.比較運算符.條件運算符 字符串的合併,用+,如果是字符串和數字用+連接,則當做字符串合併. 條件運算符:if(條件)?語句1,語句2:
- BZOJ4808马——二分图最大独立集
题目描述 众所周知,马后炮是中国象棋中很厉害的一招必杀技."马走日字".本来,如果在要去的方向有别的棋子挡住(俗 称"蹩马腿"),则不允许走过去.为了简化问题, ...
- MT【44】抛物线不常见性质3
注:S为抛物线的焦点