指數

2014-12-04 22 views
0

我需要處理電子郵件的列表,切斷他們,使輸出與迭代計數器指數

MailList = [[email protected], [email protected], [email protected]] # I have a list with emails 
# I need to process them and get next output: "User%number% is email1" I'm using something like: 
for c in MailList: 
    print('user'+ ' is '+c[0:7]) # what I need to insert here to get desirable counter for my output? 

回答

0

如果我理解正確的話,你想這樣:

MailList = ["[email protected]", "[email protected]", "[email protected]"] 

for index, value in enumerate(MailList): 
    print('user {} is {}'.format(index, value.split("@")[0])) 

the docs爲01細節。

+0

謝謝!它有幫助。 – 2014-12-05 08:40:56

+0

@AntonJakimenko我很高興我能幫助你!如果這個答案(或另一個)解決了你的問題,那麼如果你能接受它,那將是非常好的。 – rlms 2014-12-05 18:32:32

0

使用itertools.takewhile

>>> import itertools 
>>> MailList = ['[email protected]', '[email protected]', '[email protected]'] 
>>> for x in MailList: 
...  print("user is {}".format("".join(itertools.takewhile(lambda x:x!='@',x)))) 
... 
user is email1 
user is email2 
user is email3 

使用index

>>> for x in MailList: 
...  print("user is {}".format(x[:x.index('@')])) 
... 
user is email1 
user is email2 
user is email3 

使用find

>>> for x in MailList: 
...  print("user is {}".format(x[:x.find('@')])) 
... 
user is email1 
user is email2 
user is email3