簡體   English   中英

如何使用Python連接來自不同元組的兩個字符串但在同一索引中?

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

文件中的元組:

 ('Wanna', 'O')
 ('be', 'O')
 ('like', 'O')
 ('Alexander', 'B')
 ('Coughan', 'I')
 ('?', 'O')

我的問題是,如何從不同的元組加入兩個字符串,但在條件相同的索引中?

例如在我的情況下,如果[1]等於'B'並且后跟'I',我想在[0]中連接字符串

所以輸出將是:

  Alexander Coughan

這是我的代碼,但輸出不是我想要的,它只是打印“NONE”:

   readF = read_file ("a.txt")
   def jointuples(sentt, i):
        word= sentt[i][0]
        wordj = sentt[i-1][0]
        nameq = sentt[i][1]

        if nameq =='I':
           temp= ' '.join (word + wordj)
           return temp

   def join2features(sentt):
        return [jointuples(sentt, i) for i in range(len(sentt))]

   c_joint = [join2features(s) for s in readF]

   c_joint

這是我寫這個的方式:

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

原因如下:

您的文件包含的內容似乎是repr兩個字符串的Python的元組秒。 這是一個非常糟糕的格式,如果你可以改變你存儲數據的方式,你應該這樣做。 但如果為時已晚並且您必須解析它,則literal_eval是最佳答案。

因此,我們通過map ping literal_eval將文件中的每一行轉換為元組。

然后我們使用pairwiseitertools食譜到元組的迭代轉換為相鄰的一對元組的迭代。

那么,現在,在循環內部, p0p1將是來自相鄰行的元組,你可以准確地寫出你所描述的內容:如果p0[1]'B'並且它后面跟着(即p1[1]'I'join兩個[0]

我不確定你想對連接的字符串做什么,所以我只是將它打印出來。 我也不能肯定,如果你要處理多個值或只是第一個,所以我把一個break

我將擴展輸入數據以包含更多'B' + 'I'示例。

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)

結果是:

['Alexander Coughan', 'One Two']

這是一個單行解決方案

>>> 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
>>> 

當我去寫我的時候,我沒有看到@ MykhayloKopytonenko的解決方案,所以我的相似之處:

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)

這輸出:

Alexander Coughan
foo bar
>>> 

如果您必須將結果作為字符串返回,請將列表推導更改為:

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

這利用了以下事實:根據您的條件,元組列表的最后一個元素將永遠不會作為結果集的第一個元素返回。 因此, zip有效地引導您完成(tuples[n], tuples[n + 1])以便您可以輕松地檢查這些值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM