将多行添加到一个列表中?

时间:2015-07-09 16:20:34

标签: python

我有多行电子邮件,我需要做几件事:

stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu 
... etc
  1. 我需要将它们放在一个列表中:['stephen.marquard@uct.ac.za','louis@media.berkeley.edu','louis@media.berkeley.edu' ..等]
  2. 需要弄清楚哪个电子邮件是该列表中最重复的,这就是我启动代码的方式,我希望我可以从我的代码结束处完成它!

    fname = raw_input("Enter file name: ")
    if len(fname) < 1 : fname = "mbox-short.txt"
    fh = open(fname)
    lines = []
    count = 0 # For next step
    for line in fh:
        line = line.rstrip()
        if not line.startswith("From ") : continue
        x = line.split()
        emails = x[1]
     #print y
    
    maxapperence = 0 
    famous = None
    for mail in emails:
        count = emails.count(mail)
        if count > maxapperence:
            famous = mail
    print famous
    
    apparence = dict()
    for mail in set(emails):
        apparence[mail] = emails.count(mail)
    print apparence]
    

    out put:

    stephen.marquard@uct.ac.za
    louis@media.berkeley.edu
    zqian@umich.edu
    rjlowe@iupui.edu
    zqian@umich.edu
    rjlowe@iupui.edu
    cwen@iupui.edu
    cwen@iupui.edu
    gsilver@umich.edu
    gsilver@umich.edu
    zqian@umich.edu
    gsilver@umich.edu
    wagnermr@iupui.edu
    zqian@umich.edu
    antranig@caret.cam.ac.uk
    gopal.ramasammycook@gmail.com
    david.horwitz@uct.ac.za
    david.horwitz@uct.ac.za
    david.horwitz@uct.ac.za
    david.horwitz@uct.ac.za
    stephen.marquard@uct.ac.za
    louis@media.berkeley.edu
    louis@media.berkeley.edu
    ray@media.berkeley.edu
    cwen@iupui.edu
    cwen@iupui.edu
    cwen@iupui.edu
    

2 个答案:

答案 0 :(得分:1)

如果您的文件只包含电子邮件地址:

import collections
filename = ''
c = collections.Counter(map(str.strip, open(filename).readlines()))
print(c.most_common(10)) # dumb example of possible output format

答案 1 :(得分:0)

第一个例子

emails = """stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu""".split("\n")

maxapperence = 0 
famous = None
for mail in set(emails):
    count = emails.count(mail)
    if count > maxapperence:
        famous = mail
        maxapperence = count
print famous, maxapperence

您还可以存储所有邮件表情

apparence = dict()
for mail in set(emails):
    apparence[mail] = emails.count(mail)
print apparence