How to join two strings from different tuples but in the same index using Python?

前端 未结 4 1161
执笔经年
执笔经年 2021-01-22 15:07

The tuples inside the file:

 (\'Wanna\', \'O\')
 (\'be\', \'O\')
 (\'like\', \'O\')
 (\'Alexander\', \'B\')
 (\'Coughan\', \'I\')
 (\'?\', \'O\')
4条回答
  •  再見小時候
    2021-01-22 15:31

    here's a one line solution

    >>> t = [ ('wanna', 'o'),
    ... ('be', 'o'),
    ... ('like', 'o'),
    ... ('Alexander', 'B'),
    ... ('Coughan', 'I'),
    ... ('?', 'o')]
    >>> x = [B[0] for B in t if B[1]=='B'][0] + ' ' + [I[0] for I in t if I[1]=='I'][0]
    >>> print x
    Alexander Coughan
    >>> 
    

提交回复
热议问题