2017-10-08 22 views
0

我的目標是:我需要讓用戶輸入電子郵件的數量,然後啓動一個for循環來註冊輸入的電子郵件。然後,這些電子郵件將根據'@ professor.com'和'@ student.com'分類。這將在列表中作爲追加計數。以下是我已經嘗試過從Python中輸入的值中提取電子郵件

email_count = int(input('how many emails you want')) 
student_email_count = 0 
professor_email_count = 0 
student_email = '@student.com' 
professor_email = '@professor.com' 

for i in range(email_count): 
    email_list = str(input('Enter the emails') 
    if email_list.index(student_email): 
     student_email_count = student_email_count + 1 
    elif email_list.index(professor_email): 
     professor_email_count = professor_email_count + 1 

有人可以幫助縮短這個,並寫下專業解釋以供進一步參考?在這裏,追加部分丟失了。那裏有人能通過一些光線嗎?

謝謝

+0

如果你創建一個列表,然後使用它的'.append'方法將做追加...你嘗試運行你的代碼,雖然並輸入混合電子郵件 - 你已經得到另一個錯誤g Oing to crop up ... –

+0

電子郵件是一次輸入一個嗎?或多個電子郵件分隔什麼,空間? – Vinny

+0

嗨,喬恩。感謝您的反饋。我注意到只找到'@professor'索引。但是,「@學生」索引不起作用。它給出了一個錯誤,指出找不到值error-substring。這是爲什麼。如果可能的話,可否請您重寫代碼,可能會更有效率 – jeff

回答

1
prof_email_count, student_email_count = 0, 0 

for i in range(int(input("Email count # "))): 
    email = input("Email %s # " % (i+1)) 

    if email.endswith("@student.com"): # str.endswith(s) checks whether `str` ends with s, returns boolean 
     student_email_count += 1 
    elif email.endswith("@professor.com"): 
     prof_email_count += 1 

是您的代碼的(有點)縮短移交會是什麼樣子。主要區別在於我使用str.endswith(...)而不是str.index(...),我也刪除了email_count,student_emailprofessor_email變量,這些變量在上下文中的其他任何地方都沒有使用。

編輯:

要回答的可擴展性的評論,你可以實現一個系統,像這樣:

domains = { 
    "student.com": 0, 
    "professor.com": 0, 
    "assistant.com": 0 
} 

for i in range(int(input("Email count # "))): 
    email = input("Email %s # " % (i+1)) 

    try: 
     domain = email.split('@')[1] 
    except IndexError: 
     print("No/invalid domain passed") 
     continue 

    if domain not in domains: 
     print("Domain: %s, invalid." % domain) 
     continue 

    domains[domain] += 1 

它允許進一步的擴展性,你可以添加更多的域到domains詞典,訪問計數每domains[<domain>]

+0

謝謝你的代碼。這是一個簡短的版本。我可以問一下,如果可以將域名視爲列表或字典,通過循環掃描並分離值。比如,電子郵件= ['@ student.com','@ professor.com','@ assistant.com']或{'student':'@ student.com','教授':'@ professor.com', 'assistant':'@ assistant.com'};然後通過循環運行這個。 – jeff

+0

@jeff編輯答案以滿足您的要求 –

1

看來你的迭代每次只接受一封郵件;並執行email_count次。你可以使用這個簡單的代碼來統計學生和教授:

st = '@student.com' 
prof = '@professor.com' 

for i in range(email_count): 
    email = str(input('Enter the email')) 
    if st in email: 
     student_email_count += 1 
    elif prof in email: 
     professor_email_count += 1 
    else: 
     print('invalid email domain') 

如果您正在使用Python 2.7,你應該改變輸入的raw_input


這是您的代碼的可擴展版本,使用defaultdict來支持無限域。

email_count = int(input('how many emails you want')) 
student_email_count = 0 
professor_email_count = 0 

from collections import defaultdict 
domains = defaultdict(int) 

for i in range(email_count): 
    email = str(raw_input('Enter the email\n')) 
    try: 
     email_part = email.split('@')[1] 
    except IndexError: 
     print('invalid email syntax') 
    else: 
     domains[email_part] += 1 
+0

謝謝。你編碼,正在工作。不過,這是寫這個的最好方法嗎? – jeff

+0

如果您只有兩個域名和短名單的電子郵件,那麼這是一個體面的方式。它簡短易讀,易於理解。但是,這段代碼不可擴展。一旦你需要添加更多的域,你應該以不同的方式定義它。 – Vinny

+0

您建議代碼基於Python 3,但在最後一行中會出現'SyntaxError',因爲您需要調用函數來調用函數。 –

相關問題