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

前端 未结 4 1162
执笔经年
执笔经年 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
    >>> 
    
    0 讨论(0)
  • 2021-01-22 15:34

    I hadn't seen @MykhayloKopytonenko's solution when I went to write mine, so mine is similar:

    tuples = [('Wanna', 'O'),
              ('be', 'O'),
              ('like', 'O'),
              ('Alexander', 'B'),
              ('Coughan', 'I'),
              ('?', 'O'),
              ('foo', 'B'),
              ('bar', 'I'),
              ('baz', 'B'),]
    results = [(t0[0], t1[0]) for t0, t1 in zip(tuples[:-1], tuples[1:])
                              if t0[1] == 'B' and t1[1] == 'I']
    for r in results:
        print("%s %s" % r)
    

    This outputs:

    Alexander Coughan
    foo bar
    >>> 
    

    If you absolutely must have the result returned as a string, change the list comprehension to:

     results = ["%s %s" % (t0, t1) for t0, t1 in zip(tuples[:-1], tuples[1:])
                                   if t0[1] == 'B' and t1[1] == 'I']
    

    This takes advantage of the fact that, based on your criteria, the last element of your list of tuples will never be returned as the first element of the result set. As a result, the zip effectively steps you through (tuples[n], tuples[n + 1]) so that you can easily examine the values.

    0 讨论(0)
  • 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']
    
    0 讨论(0)
  • 2021-01-22 15:57

    Here's how I'd write this:

    from ast import literal_eval
    from itertools import tee
    
    def pairwise(iterable): # from itertools recipes
        a, b = tee(iterable)
        next(b, None)
        return zip(a, b)
    
    with open("a.txt") as f:
        for p0, p1 in pairwise(map(literal_eval, f)):
            if p0[1] == 'B' and p1[1] == 'I':
                print(' '.join(p0[0], p1[0]))
                break
    

    Here's why:

    Your file consists of what appear to be reprs of Python tuples of two strings. That's a really bad format, and if you can change the way you've stored your data, you should. But if it's too late and you have to parse it, literal_eval is the best answer.

    So, we turn each line in the file into a tuple by mapping literal_eval over the file.

    Then we use pairwise from the itertools recipes to convert the iterable of tuples into an iterable of adjacent pairs of tuples.

    So, now, inside the loop, p0 and p1 will be the tuples from adjacent lines, and you can just write exactly what you described: if p0[1] is 'B' and it's followed by (that is, p1[1] is) 'I', join the two [0]s.

    I'm not sure what you wanted to do with the joined string, so I just printed it out. I'm also not sure if you want to handle multiple values or just the first, so I put in a break.

    0 讨论(0)
提交回复
热议问题