题目一

'''
编写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. [转帖]Linux内核为大规模支持100Gb/s网卡准备好了吗?并没有

    Linux内核为大规模支持100Gb/s网卡准备好了吗?并没有 之前用 千兆的机器 下载速度 一般只能到 50MB 左右 没法更高 万兆的话 可能也就是 200MB左右的速度 很难更高 不知道后续的服 ...

  2. [转载]Tomcat部署与配置

    转载来源: http://ibash.cc/frontend/article/2/ 感觉挺好的  自己之前总是怕麻烦 其实是水平不够. 一句话介绍Tomcat Tomcat是一个免费的开源的Web应用 ...

  3. js控制浏览器全屏

    HTML 5中的full screen,目前可以在除IE和opera外的浏览器中使用 ,有的时候用来做全屏API,游戏呀,等都很有用.先看常见的API element.requestFullScree ...

  4. Java 数组实现 stack

    首先定义 IStack package cn.com.example.stack; /** * Created by Jack on 2017/3/8. */ public interface ISt ...

  5. Linux学习之/etc/init.d/目录和rc.local脚本

    init.d目录中包含很多系统服务的启动和停止脚本,比较常用的就是网络服务,当你修改了网络配置时,可以自行 sudo /etc/init.d/networking restart  命令来重启网络服务 ...

  6. 使用嵌入式jetty实现文件服务器

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  7. Codeforces Round#500 Div.2 翻车记

    A:签到 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  8. day9 集合基础命令

    集合的创建 s = set("hello") print(s) s = set({","alex","sb"}) print(s) ...

  9. 自学Zabbix3.12.6-动作Action-Escalations配置

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 3.12.6 自学Zabbix3.12.6-动作Action-Escalations配置 1. 概 ...

  10. KEIL中函数定义存在但go to definition却不跳转的原因

    可能是 go to definition 函数的地方,被包含在一个未使能的条件编译宏内部,因为这样KEIL在编译时,就未将该条件编译宏内部的信息编译入工程的Browse Information.