繁体   English   中英

如何优化我的代码? (建议)

[英]How can i optimize my codes ? (Suggestions)

我是 Python 的新手,我正在参加在线课程。 我必须解决的问题之一是:

10.2 编写一个程序来阅读 mbox-short.txt 并计算出每条消息在一天中每小时的分布情况。 您可以通过查找时间然后使用冒号再次拆分字符串来从“从”行中提取小时。

From xyz.abc@ab.cd.de Sat Jan 5 09:14:16 2008

一旦你积累了每小时的计数,打印出计数,按小时排序,如下所示。

我的代码是这样的:

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)

给出这个 output:

04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1

这实际上是正确的答案。 但是我确信这段代码对大多数人来说看起来很糟糕。 你对我或我未来的编码有什么建议吗? 我希望能够优化我的代码,而不仅仅是写我想到的任何东西。 对不起,如果我的英语不好。

查看 Python PEP 8以获取样式建议。

一些建议(其他人可能不同意)

  1. 如果在同一行有条件(有时可以,但不鼓励长行)

改变:

if len(name) < 1 : name = "mbox-short.txt"

至:

if len(name) < 1 : 
  name = "mbox-short.txt"
  1. 在第一次使用时初始化变量。

所以在顶部删除这些初始化,因为它们不提供上下文。

lst= []
lst2=[]
lst3=[]
ddd= {}
  1. 带有大量非描述性变量名称的代码更难遵循。

发布代码(太多非描述变量):

for l in handle:
    if l.startswith("From"):
        y= l.split()
        leg= len(y)
        if leg > 2:
            x= y[5]
            lst.append(x)

改成:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM