Python 如何优化我的代码?(建议)
我是Python新手,正在学习在线课程。我必须解决的问题之一是:Python 如何优化我的代码?(建议),python,optimization,Python,Optimization,我是Python新手,正在学习在线课程。我必须解决的问题之一是: name = input("Enter file:") if len(name) < 1 : name = "mbox-short.txt" handle = open(name) lst= [] lst2=[] lst3=[] ddd= {} for l in handle: if l.startswith("From"): y= l.split() leg= len(y)
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
lst= []
lst2=[]
lst3=[]
ddd= {}
for l in handle:
if l.startswith("From"):
y= l.split()
leg= len(y)
if leg > 2:
x= y[5]
lst.append(x)
for h in lst:
y= h.split(":")
x= y[0]
lst2.append(x)
for v in lst2:
ddd[v]= ddd.get(v,0) + 1
for k, v in ddd.items():
tup = (k, v)
lst3.append (tup)
lst3= sorted(lst3)
for k, v in lst3:
print (k, v)
10.2编写一个程序来读取mbox-short.txt,并计算出每天每小时每条消息的分布情况。通过查找时间,然后使用冒号再次拆分字符串,可以从“from”行中提取小时
来自xyz的。abc@ab.cd.de2008年1月5日星期六09:14:16
累积每小时的计数后,打印计数,按小时排序,如下所示
我的代码是:
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
lst= []
lst2=[]
lst3=[]
ddd= {}
for l in handle:
if l.startswith("From"):
y= l.split()
leg= len(y)
if leg > 2:
x= y[5]
lst.append(x)
for h in lst:
y= h.split(":")
x= y[0]
lst2.append(x)
for v in lst2:
ddd[v]= ddd.get(v,0) + 1
for k, v in ddd.items():
tup = (k, v)
lst3.append (tup)
lst3= sorted(lst3)
for k, v in lst3:
print (k, v)
这实际上是正确的答案。然而,我确信这段代码对大多数人来说都很可怕。你对我或我未来的编码有什么建议吗?我希望能够优化我的代码,而不是只写我想到的任何东西。如果我的英语不好,很抱歉。查看Python以获得风格建议
一些建议(其他人可能不同意)
if len(name) < 1 : name = "mbox-short.txt"
times = []
for line in handle:
if line.startswith("From"):
fields = line.split()
if len(fields) > 5:
times.append(fields[5])
发布的代码(非描述变量太多):
改为:
hours = []
for h in times:
hours.append(h.split(":")[0])
或者更好(使用列表理解):
重构代码
name = input("Enter file:")
if len(name) < 1 :
name = "mbox-short.txt"
with open(name) as handle: # with is preferred over plain open
times = []
for line in handle:
if line.startswith("From"):
fields = line.rstrip().split() # strip to get rid of '\n'
if len(fields) > 5:
times.append(fields[5])
hours = [h.split(":")[0] for h in times] # list comprehension
counts = {}
for v in hours:
counts[v]= counts.get(v,0) + 1
counts = sorted(counts.items()) # create tuples and sort
for k, v in counts:
print (k, v)
name=input(“输入文件:”)
如果len(名称)<1:
name=“mbox short.txt”
将open(name)作为句柄:#首选with而不是纯open
时间=[]
对于线输入句柄:
如果行开始于(“起始”):
fields=line.rstrip().split()#去除“\n”
如果len(字段)>5:
times.append(字段[5])
小时数=[h.split(“:”[0]表示时间为h]#列表理解
计数={}
对于v,以小时为单位:
计数[v]=计数。获取(v,0)+1
计数=排序(counts.items())#创建元组并排序
对于k,v的计数:
印刷品(k,v)
请分享输入的txt文件,尤其是重复介绍教程。堆栈溢出不是代码审阅站点;我们对此进行了代码审查。
times = []
for line in handle:
if line.startswith("From"):
fields = line.split()
if len(fields) > 5:
times.append(fields[5])
for h in lst:
y= h.split(":")
x= y[0]
lst2.append(x)
hours = []
for h in times:
hours.append(h.split(":")[0])
hours = [h.split(":")[0] for h in times]
name = input("Enter file:")
if len(name) < 1 :
name = "mbox-short.txt"
with open(name) as handle: # with is preferred over plain open
times = []
for line in handle:
if line.startswith("From"):
fields = line.rstrip().split() # strip to get rid of '\n'
if len(fields) > 5:
times.append(fields[5])
hours = [h.split(":")[0] for h in times] # list comprehension
counts = {}
for v in hours:
counts[v]= counts.get(v,0) + 1
counts = sorted(counts.items()) # create tuples and sort
for k, v in counts:
print (k, v)