Coursera Python Assignment的输出无效

时间:2016-02-12 15:29:18

标签: python

我正在为Everybody流做Coursera Python,而且我坚持分配10.2。我的输出无效。这是作业的要求:

  

编写一个程序来读取mbox-short.txt并找出   每天按小时分发每条消息。你可以拉   通过查找时间然后从“从”行开出小时   使用冒号第二次拆分字符串。

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008
     

累积每小时的计数后,打印出来   计数,按小时排序,如下所示。

这是我的代码:

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
lst = list()

for line in handle:
    line = line.rstrip()
    if not line.startswith('From '):
        continue
    words = line.split()
    words = words[5]
    words = words.split(":")
    for word in counts:
        counts[word] = counts.get(word, 0) + 1

lst = list()
for key, val in counts.items():
    lst.append((key, val))

lst.sort()

print lst

让我知道我做错了什么。任何建议或提示都表示赞赏。

12 个答案:

答案 0 :(得分:1)

我认为你在内循环中迭代错误的东西:它应该是for word in words,而不是for word in counts

答案 1 :(得分:1)

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

handle = open(name)

hours = dict()

for line in handle:
   if line.startswith("From "):

    hour = line.split()[5].split(':')[0] 

    hours[hour] = hours.get(hour, 0) + 1

for key, value in sorted(hours.items(), None):
    print key, value

答案 2 :(得分:1)

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)


counts = {}

for line in handle:
   if not line.startswith("From "):continue

   time = line.split()
   time = time[5]
   hour = time.split(':')
   hour = hour[0] 

   counts[hour] = counts.get(hour, 0) + 1


for k, v in sorted(counts.items()):

    print (k,v)

答案 3 :(得分:0)

counts = dict()
for line in handle:
if line.startswith ('From '):
    words = line.split()
    hour=words[5].split(':')
    counts[hour[0]]= counts.get(hour[0],0)+1
lst=list()
for key, val in counts.items():
    lst.append((key,val))
lst=sorted(lst)
for a,b in lst:
    print (a,b)

答案 4 :(得分:0)

name =input("Enter file:")
if len(name) < 1: 
    name = "mbox-short.txt"
handle = open(name)

counts = dict()

for line in handle:
    if line.startswith("From "):
        time = line.split()[5].split(":")
        counts [time[0]] = counts.get(time[0], 0) + 1

#print sorted( [ (v,k) for k,v in counts.items()] )

list = list()

for key, value in counts.items():
    list.append( (key,value) )
list.sort()

for hour, counts in list:
    print (hour, counts)

答案 5 :(得分:0)

file = open('words.txt')
dic = dict()
lst = list()


for line in file :
    line = line.rstrip()
    if not line.startswith('From '):
        continue

    words = line.split()
    words= words[5].split(':')

    words = words[0]


    dic[words] = dic.get(words,0)+1

for k,v in dic.items():
    lst.append((k,v))
lst.sort()
for k,v in lst:
    print(k,v)

答案 6 :(得分:0)

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
g={}
for line in handle :
    if not line.startswith('From'): continue
    werds=line.split()[5:6]
    for werd in werds :
      we=werd.split(':')[0]
      g[we]=g.get(we,0)+1
lst=list()
for v,k in g.items() :
    new=(v,k)
    lst.append(new)
lst=sorted(lst)
for v,k in lst :
    print(v,k)

答案 7 :(得分:0)

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
one = dict()
for line1 in handle:
    if line1.startswith("From "):
        lst1 = line1.split()
        lst2 = lst1[5].split(":")
        word = lst2[0]
        one[word] = one.get(word,0) + 1
lst3 = list()
for k,v in one.items():
    tup = (k,v)
    lst3.append(tup)
lst3 = sorted(lst3,)
for k,v in lst3:
    print(k,v)

答案 8 :(得分:0)

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
h = dict()
for line in handle:
    if line.startswith('From '):
        l = line.split()[5].split(':')[0]
        h[l] = h.get(l, 0) +1
  
for k,v in sorted(h.items(), None):
    print(k,v)

答案 9 :(得分:0)

这对我有用:-

打开文件

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)

lst = list()
counts = dict()

分割文件的每一行以获取单词,然后将单词排在第六位,然后在该第六个单词的第一个字母中将其用':'分隔,并将其附加到列表中

for lines in handle:
    if not lines.startswith('From '): continue
    words = lines.split()
    words = words[5]
    words = words.split(':')
    lst.append(words[0])

现在计数字母出现在不同的编号。的时间

for i in lst:
    counts[i] = counts.get(i,0) + 1

最后按键(此处为时间)对它们进行排序

for k, v in sorted(counts.items()):
    print(k,v)

答案 10 :(得分:0)

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

handle = open(name)
hours = dict()

for line in handle:
    # Skipping lines we don't need
    if not line.startswith("From "): 
        continue
    words = line.split()
    # Finding text in the line that we need
    time = words[5]                  
    time = time.split(":")
    hour_time = time[0]
    # Adding to the dictionary and checking if it already there
    hours[hour_time] = hours.get(hour_time, 0) + 1 

#Sorting dictionary using sorted() method
hours_sorted = sorted(hours.items())
for key, value in sorted(hours.items()):
    print(key, value)

答案 11 :(得分:-1)

name = raw_input(&#34;输入文件:&#34;) 如果len(名字)&lt; 1:name =&#34; mbox-short.txt&#34;

handle = open(name)

hours = dict()

句柄中的

行:    如果line.startswith(&#34; From&#34;):

hour = line.split()[5].split(':')[0] 

hours[hour] = hours.get(hour, 0) + 1

表示键,值为sorted(hours.items(),None):     打印键,值