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