题目一

'''
编写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. Spring MVC (Java),强制页面不缓存

    response.setDateHeader("Expires",0);        response.setHeader("Buffer","Tr ...

  2. ThreadPoolExecutor使用详解

    ThreadPoolExecutor机制  一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线 ...

  3. [日常工作] Linux与Windows的连接访问以及数据共享等方法 vncserver smb xshell xftp winscp mount等

    日常办公机器是用 windows, 但是越来越多的测试和工作需求需要使用linux. 这里以最常用的系统centos为例进行说明 1. 远程连接 ssh的方式 建议使用xmange 系列的 xshel ...

  4. Angular 行内式依赖注入

    var app = angular.module('myApp', ['ng']); //创建一个自定义服务app.factory('$Debug', function () { return { d ...

  5. React 模板

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  6. ubuntu更改分辨率

    1. 输入:$cvt 1920 1080 2 输入: $xrandr 3 输入: $sudo xrandr --newmode "1920x1080_60.00" 173.00 1 ...

  7. 配置自己的Maven方式并使用Maven 运行项目Idea的maven的项目

    (1) 当安装了 maven之后,需要导入项目代码,然后编译执行: 打开Idea ==>然后点击小扳手==>在搜索框中输入maven==>然后找到 Maven home direct ...

  8. Lodop图片输出ADD_PRINT_IMAGE 有白边

    ADD_PRINT_IMAGE输出图片,如果使用img标签(即超文本<img标签),是超文本,无论是相对路径,网络图片,还是base64,都可能有白边,这可能和超文本解析有关.ADD_PRINT ...

  9. Nginx referer防盗链模块

    L75 referer模块 ngx_http_referer_module 默认编译进nginx valid_referers 指令 Syntax: valid_referers none | blo ...

  10. BZOJ1022[SHOI2008]小约翰的游戏——anti-SG(反尼姆博弈)

    题目描述 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到 ...