给定一个字符串元组,计算由包含字符@的t中的所有字符串组成的元组

时间:2014-07-19 08:21:23

标签: python python-2.7 tuples

我需要一条'一线' Python语句,它生成一个元组,只包含带有' @'的字符串。在他们中间。


Input:
    tuple_1 = ('asdf@', 'asdf')      #the given (not included in the 2 lines)

我的尝试:2行

for string in tuple_1:                       
    if '@' in string: tuple_2 = (string)

Out: 
    tuple_2 = ('asdf@')

1 个答案:

答案 0 :(得分:1)

经过几次试训,我刚刚解决了这个问题:

tuple_1 = ('asdf@', 'asdf')

tuple_2 = ()

for elem in [item for item in tuple_1 if '@' in item]: tuple_2 = tuple_2 + (elem,)

print tuple_2

我希望这就是你所需要的!我投票给你了,因为这让我想起了大学里数学自学时光的一点点乐趣。