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

前端 未结 4 1203
执笔经年
执笔经年 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:41

    I'll extend the input data to include more 'B' + 'I' examples.

    phrases = [('Wanna', 'O'),
        ('be', 'O'),
        ('like', 'O'),
        ('Alexander', 'B'),
        ('Coughan', 'I'),
        ('One', 'B'),
        ('Two', 'I'),
        ('Three', 'B')]
    
    length = len(phrases)
    res = ['%s %s' % (phrases[i][0], phrases[i + 1][0])
        for i in range(length)
        if i < length - 1 and phrases[i][1] == 'B' and phrases[i + 1][1] == 'I']
    print(res)
    

    The result is:

    ['Alexander Coughan', 'One Two']
    

提交回复
热议问题