[英]convert this “list of a list the tuple” with 2 elements to a “list of tuple” with 3 elements
我想將這個帶有 2 個元素的“元組列表”轉換為帶有 3 個元素的“元組列表”
[([('Yes', 'UH'),
(',', ','),
('it', 'PRP'),
("'s", 'VBZ'),
('annoying', 'JJ'),
('and', 'CC'),
('cumbersome', 'JJ'),
('to', 'TO'),
('separate', 'VB'),
('your', 'PRP$'),
('rubbish', 'NN'),
('properly', 'RB'),
('all', 'PDT'),
('the', 'DT'),
('time', 'NN'),
('.', '.')],
'P'),
([('Three', 'CD'),
('different', 'JJ'),
('bin', 'JJ'),
('bags', 'NNS'),
('stink', 'VBP'),
('away', 'RB'),
('in', 'IN'),
('the', 'DT'),
('kitchen', 'NN'),
('and', 'CC'),
('have', 'VB'),
('to', 'TO'),
('be', 'VB'),
('sorted', 'VBN'),
('into', 'IN'),
('different', 'JJ'),
('wheelie', 'NN'),
('bins', 'NNS'),
('.', '.')],
'P'),
([('But', 'CC'),
('still', 'RB'),
('Germany', 'NNP'),
('produces', 'VBZ'),
('way', 'RB'),
('too', 'RB'),
('much', 'JJ'),
('rubbish', 'NN')],
'P'),
([('and', 'CC'),
('too', 'RB'),
('many', 'JJ'),
('resources', 'NNS'),
('are', 'VBP'),
('lost', 'VBN'),
('when', 'WRB'),
('what', 'WP'),
('actually', 'RB'),
('should', 'MD'),
('be', 'VB'),
('separated', 'VBN'),
('and', 'CC'),
('recycled', 'VBN'),
('is', 'VBZ'),
('burnt', 'VBN'),
('.', '.')],
'P'),
([('We', 'PRP'),
('Berliners', 'NNS'),
('should', 'MD'),
('take', 'VB'),
('the', 'DT'),
('chance', 'NN'),
('and', 'CC'),
('become', 'VB'),
('pioneers', 'NNS'),
('in', 'IN'),
('waste', 'NN'),
('separation', 'NN'),
('!', '.')],
'C')]
到這個列表
[('Yes', 'UH', 'B-P'),
(',', ',','I-P'),
('it', 'PRP','I-P'),
("'s", 'VBZ','I-P'),
('annoying', 'JJ','I-P'),
('and', 'CC','I-P'),
('cumbersome', 'JJ','I-P'),
('to', 'TO', 'I-P'),
('separate', 'VB', 'I-P'),
('your', 'PRP$','I-P'),
('rubbish', 'NN','I-P'),
('properly', 'RB','I-P'),
('all', 'PDT','I-P'),
('the', 'DT','I-P'),
('time', 'NN','I-P'),
('.', '.','I-P')],
.
.
.
.
([('We', 'PRP','B-C'),
('Berliners', 'NNS','I-C'),
('should', 'MD','I-C'),
('take', 'VB','I-C'),
('the', 'DT','I-C'),
('chance', 'NN','I-C'),
('and', 'CC','I-C'),
('become', 'VB','I-C'),
('pioneers', 'NNS','I-C'),
('in', 'IN','I-C'),
('waste', 'NN','I-C'),
('separation', 'NN','I-C'),
('!', '.','I-C')]
如你所見,
我們到處都有 label P---> 我們添加 label BP(列表的開始令牌)和 IP 作為元組的 3d 成員
我們到處都有 label C---> 我們添加了一個 label BC(列表的開始令牌)和 IP 作為 3d 元組的成員,他們稱此為 B 標記
我嘗試了不同的方法仍然找不到解決方案
listtoken=[]
listsent=[]
for lst in a:
for tpl,l in zip(lst,b):
c=(*tpl, l)
listtoken.append(c)
listsent.append(listtoken)
要將單個項目添加到元組,您可以將+
與單元素元組一起使用(表示為(element,)
)。 所以(1,2)+(3,) => (1,2,3)
列表理解應該很容易完成這項工作:
# with your list as L
R = [ [t+('BI'[i>0]+'-'+lb,) for i,t in enumerate(T)] for T,lb in L ]
output:
print(R)
[
[ ('Yes', 'UH', 'B-P'), (',', ',', 'I-P'), ('it', 'PRP', 'I-P'), ("'s", 'VBZ', 'I-P'), ('annoying', 'JJ', 'I-P'), ('and', 'CC', 'I-P'), ('cumbersome', 'JJ', 'I-P'), ('to', 'TO', 'I-P'), ('separate', 'VB', 'I-P'), ('your', 'PRP$', 'I-P'), ('rubbish', 'NN', 'I-P'), ('properly', 'RB', 'I-P'), ('all', 'PDT', 'I-P'), ('the', 'DT', 'I-P'), ('time', 'NN', 'I-P'), ('.', '.', 'I-P')],
[ ('Three', 'CD', 'B-P'), ('different', 'JJ', 'I-P'), ('bin', 'JJ', 'I-P'), ('bags', 'NNS', 'I-P'), ('stink', 'VBP', 'I-P'), ('away', 'RB', 'I-P'), ('in', 'IN', 'I-P'), ('the', 'DT', 'I-P'), ('kitchen', 'NN', 'I-P'), ('and', 'CC', 'I-P'), ('have', 'VB', 'I-P'), ('to', 'TO', 'I-P'), ('be', 'VB', 'I-P'), ('sorted', 'VBN', 'I-P'), ('into', 'IN', 'I-P'), ('different', 'JJ', 'I-P'), ('wheelie', 'NN', 'I-P'), ('bins', 'NNS', 'I-P'), ('.', '.', 'I-P')],
[ ('But', 'CC', 'B-P'), ('still', 'RB', 'I-P'), ('Germany', 'NNP', 'I-P'), ('produces', 'VBZ', 'I-P'), ('way', 'RB', 'I-P'), ('too', 'RB', 'I-P'), ('much', 'JJ', 'I-P'), ('rubbish', 'NN', 'I-P')],
[ ('and', 'CC', 'B-P'), ('too', 'RB', 'I-P'), ('many', 'JJ', 'I-P'), ('resources', 'NNS', 'I-P'), ('are', 'VBP', 'I-P'), ('lost', 'VBN', 'I-P'), ('when', 'WRB', 'I-P'), ('what', 'WP', 'I-P'), ('actually', 'RB', 'I-P'), ('should', 'MD', 'I-P'), ('be', 'VB', 'I-P'), ('separated', 'VBN', 'I-P'), ('and', 'CC', 'I-P'), ('recycled', 'VBN', 'I-P'), ('is', 'VBZ', 'I-P'), ('burnt', 'VBN', 'I-P'), ('.', '.', 'I-P')],
[ ('We', 'PRP', 'B-C'), ('Berliners', 'NNS', 'I-C'), ('should', 'MD', 'I-C'), ('take', 'VB', 'I-C'), ('the', 'DT', 'I-C'), ('chance', 'NN', 'I-C'), ('and', 'CC', 'I-C'), ('become', 'VB', 'I-C'), ('pioneers', 'NNS', 'I-C'), ('in', 'IN', 'I-C'), ('waste', 'NN', 'I-C'), ('separation', 'NN', 'I-C'), ('!', '.', 'I-C')]
]
您可以在解包中使用列表推導:
d = [([('Yes', 'UH'), (',', ','), ('it', 'PRP'), ("'s", 'VBZ'), ('annoying', 'JJ'), ('and', 'CC'), ('cumbersome', 'JJ'), ('to', 'TO'), ('separate', 'VB'), ('your', 'PRP$'), ('rubbish', 'NN'), ('properly', 'RB'), ('all', 'PDT'), ('the', 'DT'), ('time', 'NN'), ('.', '.')], 'P'), ([('Three', 'CD'), ('different', 'JJ'), ('bin', 'JJ'), ('bags', 'NNS'), ('stink', 'VBP'), ('away', 'RB'), ('in', 'IN'), ('the', 'DT'), ('kitchen', 'NN'), ('and', 'CC'), ('have', 'VB'), ('to', 'TO'), ('be', 'VB'), ('sorted', 'VBN'), ('into', 'IN'), ('different', 'JJ'), ('wheelie', 'NN'), ('bins', 'NNS'), ('.', '.')], 'P'), ([('But', 'CC'), ('still', 'RB'), ('Germany', 'NNP'), ('produces', 'VBZ'), ('way', 'RB'), ('too', 'RB'), ('much', 'JJ'), ('rubbish', 'NN')], 'P'), ([('and', 'CC'), ('too', 'RB'), ('many', 'JJ'), ('resources', 'NNS'), ('are', 'VBP'), ('lost', 'VBN'), ('when', 'WRB'), ('what', 'WP'), ('actually', 'RB'), ('should', 'MD'), ('be', 'VB'), ('separated', 'VBN'), ('and', 'CC'), ('recycled', 'VBN'), ('is', 'VBZ'), ('burnt', 'VBN'), ('.', '.')], 'P'), ([('We', 'PRP'), ('Berliners', 'NNS'), ('should', 'MD'), ('take', 'VB'), ('the', 'DT'), ('chance', 'NN'), ('and', 'CC'), ('become', 'VB'), ('pioneers', 'NNS'), ('in', 'IN'), ('waste', 'NN'), ('separation', 'NN'), ('!', '.')], 'C')]
new_d = [[(*a, f'B-{c}'), *[(*j, f'I-{c}') for j in b]] for [a, *b], c in d]
Output:
[[('Yes', 'UH', 'B-P'), (',', ',', 'I-P'), ('it', 'PRP', 'I-P'), ("'s", 'VBZ', 'I-P'), ('annoying', 'JJ', 'I-P'), ('and', 'CC', 'I-P'), ('cumbersome', 'JJ', 'I-P'), ('to', 'TO', 'I-P'), ('separate', 'VB', 'I-P'), ('your', 'PRP$', 'I-P'), ('rubbish', 'NN', 'I-P'), ('properly', 'RB', 'I-P'), ('all', 'PDT', 'I-P'), ('the', 'DT', 'I-P'), ('time', 'NN', 'I-P'), ('.', '.', 'I-P')], [('Three', 'CD', 'B-P'), ('different', 'JJ', 'I-P'), ('bin', 'JJ', 'I-P'), ('bags', 'NNS', 'I-P'), ('stink', 'VBP', 'I-P'), ('away', 'RB', 'I-P'), ('in', 'IN', 'I-P'), ('the', 'DT', 'I-P'), ('kitchen', 'NN', 'I-P'), ('and', 'CC', 'I-P'), ('have', 'VB', 'I-P'), ('to', 'TO', 'I-P'), ('be', 'VB', 'I-P'), ('sorted', 'VBN', 'I-P'), ('into', 'IN', 'I-P'), ('different', 'JJ', 'I-P'), ('wheelie', 'NN', 'I-P'), ('bins', 'NNS', 'I-P'), ('.', '.', 'I-P')], [('But', 'CC', 'B-P'), ('still', 'RB', 'I-P'), ('Germany', 'NNP', 'I-P'), ('produces', 'VBZ', 'I-P'), ('way', 'RB', 'I-P'), ('too', 'RB', 'I-P'), ('much', 'JJ', 'I-P'), ('rubbish', 'NN', 'I-P')], [('and', 'CC', 'B-P'), ('too', 'RB', 'I-P'), ('many', 'JJ', 'I-P'), ('resources', 'NNS', 'I-P'), ('are', 'VBP', 'I-P'), ('lost', 'VBN', 'I-P'), ('when', 'WRB', 'I-P'), ('what', 'WP', 'I-P'), ('actually', 'RB', 'I-P'), ('should', 'MD', 'I-P'), ('be', 'VB', 'I-P'), ('separated', 'VBN', 'I-P'), ('and', 'CC', 'I-P'), ('recycled', 'VBN', 'I-P'), ('is', 'VBZ', 'I-P'), ('burnt', 'VBN', 'I-P'), ('.', '.', 'I-P')], [('We', 'PRP', 'B-C'), ('Berliners', 'NNS', 'I-C'), ('should', 'MD', 'I-C'), ('take', 'VB', 'I-C'), ('the', 'DT', 'I-C'), ('chance', 'NN', 'I-C'), ('and', 'CC', 'I-C'), ('become', 'VB', 'I-C'), ('pioneers', 'NNS', 'I-C'), ('in', 'IN', 'I-C'), ('waste', 'NN', 'I-C'), ('separation', 'NN', 'I-C'), ('!', '.', 'I-C')]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.