【问题标题】:Given a tuple t of strings, compute the tuple consisting of all strings in t containing the character @给定一个字符串元组 t,计算由 t 中包含字符 @ 的所有字符串组成的元组
【发布时间】:2014-09-10 09:02:13
【问题描述】:

我需要一个“单行”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@')

【问题讨论】:

    标签: python python-2.7 tuples


    【解决方案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
    

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

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多