python -- 题目不看别人的自己写然后比较

时间:2022-11-10 19:47:53

题目一

'''
编写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&lpar;&quot&semi;&quot&semi;&comma;&dollar;numbers&rpar;&semi;)(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. html基础 2

    HTML 文本格式化实例 (我不知道为什么“正常显示文本”这几个字不用加标签,虽然它有在<body>标签内) <html> <body> <b>文本为黑 ...

  2. Asp&period;net NVelocity 模版引擎

    NVelocity.dll是Java中常用的一个模版,下面是常用的模版引擎 1,返回string类型的html代码 /// <summary> /// 获取html模版 /// </ ...

  3. SQL语句汇总&lpar;二&rpar;——数据修改、数据查询

    首先创建一张表如下,创建表的方法在上篇介绍过了,这里就不再赘述. 添加新数据: INSERT INTO <表名> (<列名列表>) VALUES (<值列表>)  ...

  4. c&num; 图片XML序列化与反序列化

    var xmlDoc = new XmlDocument(); xmlDoc.Load(@"C:\Users\*\Desktop\*.xml"); ].ChildNodes[]; ...

  5. iOS 进阶 第五天&lpar;0330&rpar;

    0330 cell的一些常见属性 设置cell右边指示器的类型 设置cell右边指示器的view cell的backgroundView和selectedBackgroundView cell的bac ...

  6. Let&&num;39&semi;s go home

    hdu1824:http://acm.hdu.edu.cn/showproblem.php?pid=1824 题意:中文题. 题解:这一题建边要考虑两个限制条件,一个是队伍内部的,就是假如说 a,b, ...

  7. Weka初步

    从前年開始使用weka最数据挖掘方面的研究,到如今有一年半的时间了.看到我们同组的兄弟写了关于weka方面的总结.我也想整理一下.由于网上的资料实在是太少.记得刚接手的时候,真是硬着头皮看代码.只是到 ...

  8. Java中的集合类型的继承关系图

    Java中的集合类型的继承关系图

  9. SSH:dataSource配置问题

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  10. centos yum源配置 与yum配置文件

    参考博客 http://www.cnblogs.com/mchina/archive/2013/01/04/2842275.html 1.centos . yum配置文件在目录 /etc/yum.re ...