2014-12-04 3 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 подробную информацию о enumerate.

+0

Спасибо! Это помогло. –

+0

@ Антон Якименко Я рад, что помогу вам! Если бы этот ответ (или другой) решил вашу проблему, было бы здорово, если бы вы его приняли. – rlms

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 
Смежные вопросы