Python:coursera赋值 - 词典章节

时间:2016-09-27 11:03:01

标签: python dictionary

我试图从课程中解决作业 - Python。

编写一个程序来读取mbox-short.txt并找出谁发送了最多的邮件消息。该计划寻找' From'行并将这些行的第二个单词作为发送邮件的人。该程序创建一个Python字典,将发件人的邮件地址映射到它们在文件中出现的次数。在生成字典之后,程序使用最大循环读取字典以找到最多产的提交者。

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

lst = list()
for line in handle:
    line = line.strip()
    if line.startswith("From"):
        words = line.split()
        email = words[1]
        lst.append(email)

dct = dict()
for email in lst:
    dct[email] = dct.get(email,0)+1

bigcount = None
email_address = None
for key,value in dct.items():
    if bigcount is None or value > bigcount:
        bigcount = value
        email_address = key

print email_address, bigcount

我的代码运行,但所需的输出应该是:cwen@iupui.edu 5,但我得到某种方式&#34;加倍&#34;输出:cwen@iupui.edu 10

有人解决了这个问题吗?能不能给我一个暗示我错过了什么?非常感谢你!

9 个答案:

答案 0 :(得分:1)

 name = input("Enter file:")
 if len(name) < 1 : name = "mbox-short.txt"
 handle = open(name)
 word=list()
 for line in handle:
     if line.startswith('From'):
        words=line.split()
        word.append(words)

count=dict()
for word in words:
count[word]=count.get(word,0)+1

bigcount = None
bigword= None
for count,word in count.items():
    if count > bigcount:
    bigcount=count
    bigword=word

print(bigcount,bigword)        

答案 1 :(得分:0)

检查第3726行和第3763行。后面有一个冒号。我认为你在手动搜索时失踪了。

  

3726行来自cwen@iupui.edu Thu Jan 3 16:23:48 2008

     

第3763行来自:cwen@iupui.edu

否则代码是正确的。它显示正确的输出。

答案 2 :(得分:0)

这个任务的正确答案是5,而不是10.正如qmaruf所指出的那样,有有和没有冒号的行。赋值状态为查找以&#34; From&#34;开头的所有行。您正在查找以&#34; From&#34;开头的所有行。你看到了区别吗?

答案 3 :(得分:0)

fname = input("Enter file name: ")
counts = dict()
handle = open(fname)
for line in handle:
   line=line.rstrip()
   if line.startswith('From '):
       words=line.split()
       counts[words[1]]=counts.get(words[1],0)+1
bigcount=0
for word,count in counts.items():
   if bigcount == 0 or count>bigcount:
       bigword=word
       bigcount=count

print(bigword,bigcount)

答案 4 :(得分:0)

fname = input('enter the file name')
fh = open(fname)
counts = dict()
for line in fh:
    if line.startswith('From'):
        words = line.split()
        for word in words :
            counts[words[1]]= counts.get(words[1],0)+1

bigcount=0
for word,count in counts.items():
   if bigcount == 0 or count>bigcount:
       bigword=word
       bigcount=count

print(bigword,bigcount)

这是非常简单易懂的,所以使用它。

答案 5 :(得分:0)

您可以尝试以下代码段:

name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
counts=dict()
handle = open(name)
for line in handle:
    if line.startswith('From '):
        line=line.split()
        counts[line[1]]=counts.get(line[1],0)+1
bigword=None
bigcount=0
for key,val in counts.items():
  if bigcount==0 or val>bigcount : 
        bigcount=val
        bigword=key
print(bigword,bigcount)

答案 6 :(得分:0)

此代码有效:

counts = dict()
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
fhand = open(name)
for line in fhand:
       line = line.rstrip()
       if not line.startswith('From ') : continue
       words = line.split()            
       counts[words[1]]=counts.get(words[1],0)+1 
st = 0 
for k in counts:
       if counts[k] > st :
       st = counts[k]
       addy = k
print (addy, st)

答案 7 :(得分:0)

word=list()
count=dict()
name = input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
for line in handle:
    if not line.startswith("From ") :
        continue
    words=line.split()
    word.append(words[1])#gets the emails from list
for name in word:
    count[name]=count.get(name,0)+1
bigemail=bigcount=None
for word,count in count.items():
    if bigcount is None or count > bigcount:
        bigcount=count
        bigemail=word
print(bigemail,bigcount)

答案 8 :(得分:0)

只需仔细看看运动。 他们已经说过,您应该使用所有以”来自“ ”开头的字符串,而不是“来自”