简体   繁体   English

如何通过列表理解加入列表元组的元素

[英]How do I join elements of tuples of a list through list comprehension

I have a list containing tuples of three elements which contain e-mail data.我有一个列表,其中包含三个元素的元组,其中包含电子邮件数据。

email_data = [('jbd', 'email', '.com'), ('my_jbd', 'my_site', '.com')]

What I am trying to attain is to join three elements of each tuple and get email address like 'jbd@email.com' and for that I am using list comprehension as under:我想要实现的是加入每个元组的三个元素并获得 email 地址,例如“jbd@email.com”,为此我使用如下列表理解:

email_list = [  (y+'@', y) [i!=0] for x in result for i, y in enumerate(x) ]
print( email_list ) # ['jbd@', 'email', '.com', 'my_jbd@', 'my_site', '.com']

What I exactly require is list like this -> ['jbd@email.com', 'my_jbd@my_site.com'].我真正需要的是这样的列表-> ['jbd@email.com', 'my_jbd@my_site.com']。
Since, I am a beginner in Python, I am clueless as to how do I join these tuple elements within list comprehension and get a list containing email data.因为,我是 Python 的初学者,所以我不知道如何在列表理解中加入这些元组元素并获得包含 email 数据的列表。 Please guide me.请指导我。

Use the format method.使用format方法。

email_list = ["{}@{}{}".format(*t) for t in email_data]

Use:利用:

email_data=[i[0]+"@"+i[1]+i[2] for i in email_data]
print(email_data)

This should work:这应该有效:

[f'{el[0]}@{el[1]}{el[2]}' for el in email_data]

What we are doing here is using a format string to pass in each tuple which we are naming 'el' in the list comprehension.我们在这里所做的是使用格式字符串来传递我们在列表理解中命名为“el”的每个元组。

Try:尝试:

email_data = [('jbd', 'email', '.com'), ('my_jbd', 'my_site', '.com')]
for i in email_data :
    s = i[0] + '@'+''.join(i[1:])
    print(s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM