题目一

'''
编写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 -- 题目不看别人的自己写然后比较的更多相关文章

  1. 自学笔记系列:《Python学习手册 第五版》 -写在开始之前

    今年双十一,在当当网上买了这本书,很厚很厚的一本书,大概有将近1700页左右,的确是一个“大工程”, 关于这本书的学习,我想采用一种博客的方式进行,既是写给自己,也想分享给每一个对Python学习感兴 ...

  2. 看别人的代码学习的css

    <ul class='y1'>      <li><a href="#">菜单</a></li>      <li ...

  3. 看源码和写demo是一种比较容易提升的方式

    github就是要这么用才行.看别人的源码,就能了解到很多规范,而写demo,就是自己写出自己的代码.莫欺少年穷

  4. Python初学者必看(1)

    python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  5. php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort)

    php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort) 一.总结 核心是排序 ...

  6. 之前同事问到的一道python题目

    Python面试题 之前同事问了一道Python题目如下,暂时归类为面试题 题目:把类似'123.456'的字符串转换成浮点型数据 方法一: >>> print '{:.3f}'.f ...

  7. 看了xici有写给孩子的信,maybe我也要写给孩子一些东西了

    看了xici有写给孩子的信,maybe我也要写给孩子一些东西了

  8. 孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5

    孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...

  9. 孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4

    孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...

随机推荐

  1. 面象对象设计原则之六:迪米特原则(LeastKnowledge Principle, LKP)

    迪米特法则来自于1987年美国东北大学(Northeastern University)一个名为“Demeter”的研究项目.迪米特法则又称为最少知识原则(LeastKnowledge Princip ...

  2. Node post请求 通常配合ajax

    //处理客户post请求//*1:加载相应模块 http express querystring//*2:创建web服务器//*3:监听端口8080const http = require(" ...

  3. HMM模型学习笔记(维特比算法)

    维特比算法(Viterbi) 维特比算法  编辑 维特比算法是一种动态规划算法用于寻找最有可能产生观测事件序列的-维特比路径-隐含状态序列,特别是在马尔可夫信息源上下文和隐马尔可夫模型中.术语“维特比 ...

  4. 在腾讯云&阿里云上部署JavaWeb项目(Tomcat+MySQL)

    之前做项目都是在本地跑,最近遇到需要在在云服务器(阿里云或者腾讯云都可以,差不多)上部署Java Web项目的问题,一路上遇到了好多坑,在成功部署上去之后写一下部署的步骤与过程,一是帮助自己总结记忆, ...

  5. SESSION和cookie的使用和区别

    PHP中SESSION和cookie的使用和区别 cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制. PHP在http协议的头信息里发送cookie, 因此 setcookie( ...

  6. share.js轻松分享/邀请

    GitHub地址 https://github.com/overtrue/share.js 安装 安装的方法很多,大家选择自己合适的进行安装就好. clone $ git clone https:// ...

  7. 自学Linux Shell15.1-处理信号

    点击返回 自学Linux命令行与Shell脚本之路 15.1-处理信号 Linux使用信号与系统上运行的进程进行通信.可以使用这些信号控制Shell脚本的运行,只需要让shell脚本在接收到来自Lin ...

  8. 自学Zabbix3.10.2.1 linux如何配置使用sendEmail发送邮件

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.10.2.1 linux如何配置使用sendEmail发送邮件 sendEma ...

  9. bzoj4336 骑士的旅行 (树链剖分+multiset)

    首先大概有一个树剖+树套树的做法,但我哪会写啊 然后发现k很小,如果用线段树记每个区间前k大的的话,可以O(k)地合并 而且一个点还有可能有好多个骑士,所以要用multiset维护一下 然后树剖就好啦 ...

  10. enumerate()用法

    语法: enumerate(sequence,[start=0]) test = [i for i in range(9)] for i in test: print(i) for i,j in en ...