Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 给定字符串的元组t,计算由t中包含该字符的所有字符串组成的元组@_Python_Python 2.7_Tuples - Fatal编程技术网

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

Python 给定字符串的元组t,计算由t中包含该字符的所有字符串组成的元组@,python,python-2.7,tuples,Python,Python 2.7,Tuples,我需要一个“一行”Python语句,它生成一个元组,其中只包含带“@”的字符串 我的尝试:2行 经过几次试用,我刚刚解决了这个问题: 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 我希望这就是你需要的!我投票支持你,因为这让我想起了我在大学里的数学自学时代,

我需要一个“一行”Python语句,它生成一个元组,其中只包含带“@”的字符串


我的尝试:2行

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

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
我希望这就是你需要的!我投票支持你,因为这让我想起了我在大学里的数学自学时代,这有点好玩

for string in tuple_1:                       
    if '@' in string: tuple_2 = (string)
Out: 
    tuple_2 = ('asdf@')
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