简体   繁体   English

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

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

Im new to Python and im taking an online course.我是 Python 的新手,我正在参加在线课程。 One of the problems i have to solve is this:我必须解决的问题之一是:

10.2 Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. 10.2 编写一个程序来阅读 mbox-short.txt 并计算出每条消息在一天中每小时的分布情况。 You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.您可以通过查找时间然后使用冒号再次拆分字符串来从“从”行中提取小时。

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

Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below.一旦你积累了每小时的计数,打印出计数,按小时排序,如下所示。

My code is this:我的代码是这样的:

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)

giving this output:给出这个 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

Which is actually the correct answer.这实际上是正确的答案。 However im sure this code looks horrible for most people.但是我确信这段代码对大多数人来说看起来很糟糕。 Do you have any suggestions for me or my future coding?你对我或我未来的编码有什么建议吗? I want to be able to optimize my codes and not just write whatever come to my mind.我希望能够优化我的代码,而不仅仅是写我想到的任何东西。 Sorry if my english is bad.对不起,如果我的英语不好。

Checkout Python PEP 8 for style suggestions.查看 Python PEP 8以获取样式建议。

Some suggestions (others may disagree)一些建议(其他人可能不同意)

  1. If conditional on same line (sometimes okay but discouraged for long lines)如果在同一行有条件(有时可以,但不鼓励长行)

Change:改变:

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

To:至:

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

So remove these initializations at the top since they provide no context.所以在顶部删除这些初始化,因为它们不提供上下文。

lst= []
lst2=[]
lst3=[]
ddd= {}
  1. Code is harder to follow with lots of non-descript variable names.带有大量非描述性变量名称的代码更难遵循。

Posted code (too many non-descript variables):发布代码(太多非描述变量):

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

Change to:改成:

times = []
for line in handle:
    if line.startswith("From"):
      fields = line.split()
      if len(fields) > 5:
        times.append(fields[5])

Posted code (too many non-descript variables):发布代码(太多非描述变量):

for h in lst:
  y= h.split(":")
  x= y[0]
  lst2.append(x)

Change To:改成:

hours = []
for h in times:
  hours.append(h.split(":")[0])

Or even better (use list comprehension):甚至更好(使用列表理解):

hours = [h.split(":")[0] for h in times]

Refactored Code重构代码

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