Python:重建嵌套的lis

2024-03-28 19:14:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个嵌套列表:

  output= [('the', 'B', 'NNP'), ('wall', 'I', 'NNP'), ('street', 'I', 'NNP'), ('journal', 'I', 'NNP'), ('reported', 'O', 'VB'), ('today', 'O', 'NNP'), ('that', 'O', 'NNP'), ('apple', 'B', 'NNP'), ('corporation', 'I', 'NNP'), ('made', 'O', 'VB'), ('money', 'O', 'NNP'), ('.', 'O', '.'), ('georgia', 'B', 'NNP'), ('tech', 'I', 'NNP'), ('is', 'O', 'NNP'), ('a', 'O', '.'), ('university', 'O', 'NNP'), ('in', 'O', 'NNP'), ('georgia', 'B', 'NNP'),('.', 'O', '.')]

我想将其重新格式化为以下预期格式:

new_output= [(['the', 'wall', 'street', 'journal', 'reported', 'today', 'that', 'apple', 'corporation', 'made', 'money'], ['B', 'I', 'I', 'I', 'O', 'O', 'O', 'B', 'I', 'O', 'O']), (['georgia', 'tech', 'is', 'a', 'university', 'in', 'georgia'], ['B', 'I', 'O', 'O', 'O', 'O', 'B'])]

我的尝试是:

import string
word = []
token = []
result_word = []
result_token = []

result = []
for i in output[0]:
    for every_word in i:
        word.append(every_word)
result_word = " ".join(" ".join(word).split()[::3])

如何获得预期格式?你知道吗


Tags: theinstreetappleoutputtodaythatresult
3条回答
output = [('the', 'B', 'NNP'), ('wall', 'I', 'NNP'), ('street', 'I', 'NNP'), ('journal', 'I', 'NNP'), ('reported', 'O', 'VB'), ('today', 'O', 'NNP'), ('that', 'O', 'NNP'), ('apple', 'B', 'NNP'), ('corporation', 'I', 'NNP'), ('made', 'O', 'VB'), ('money', 'O', 'NNP'), ('.', 'O', '.'), ('georgia', 'B', 'NNP'), ('tech', 'I', 'NNP'), ('is', 'O', 'NNP'), ('a', 'O', '.'), ('university', 'O', 'NNP'), ('in', 'O', 'NNP'), ('georgia', 'B', 'NNP'),('.', 'O', '.')]
result, words, tokens = [], [], []
for word, token, _ in output:  # this is tuple like ('the', 'B', 'NNP')
    if word == '.':   # end of sentence, save current and start new one
        result.append((words, tokens))
        words, tokens = [], []
    else:   # add new word to current sentence
        words.append(word)
        tokens.append(token)

print(result)

输出:

[(['the', 'wall', 'street', 'journal', 'reported', 'today', 'that', 'apple', 'corporation', 'made', 'money'], ['B', 'I', 'I', 'I', 'O', 'O', 'O', 'B', 'I', 'O', 'O']), (['georgia', 'tech', 'is', 'a', 'university', 'in', 'georgia'], ['B', 'I', 'O', 'O', 'O', 'O', 'B'])]

您可以使用^{}将非句点项分组成句子,然后使用zip将词性指示符中的单词分开:

from itertools import groupby

l =   output= [('the', 'B', 'NNP'), ('wall', 'I', 'NNP'), ('street', 'I', 'NNP'), ('journal', 'I', 'NNP'), ('reported', 'O', 'VB'), ('today', 'O', 'NNP'), ('that', 'O', 'NNP'), ('apple', 'B', 'NNP'), ('corporation', 'I', 'NNP'), ('made', 'O', 'VB'), ('money', 'O', 'NNP'), ('.', 'O', '.'), ('georgia', 'B', 'NNP'), ('tech', 'I', 'NNP'), ('is', 'O', 'NNP'), ('a', 'O', '.'), ('university', 'O', 'NNP'), ('in', 'O', 'NNP'), ('georgia', 'B', 'NNP'),('.', 'O', '.')]


groups = (g for k, g in groupby(l, lambda x: x[0] != '.') if k)
zs = (zip(*g) for g in groups)
res = [(next(z), next(z)) for z in zs]

res就是这样

[(('the', 'wall', 'street', 'journal', 'reported', 'today', 'that', 'apple', 'corporation', 'made', 'money'), 
  ('B', 'I', 'I', 'I', 'O', 'O', 'O', 'B', 'I', 'O', 'O')), 
 (('georgia', 'tech', 'is', 'a', 'university', 'in', 'georgia'), 
  ('B', 'I', 'O', 'O', 'O', 'O', 'B'))
]

你可以这样做:

from itertools import groupby
from operator import itemgetter

output = [('the', 'B', 'NNP'), ('wall', 'I', 'NNP'), ('street', 'I', 'NNP'), ('journal', 'I', 'NNP'),
          ('reported', 'O', 'VB'), ('today', 'O', 'NNP'), ('that', 'O', 'NNP'), ('apple', 'B', 'NNP'),
          ('corporation', 'I', 'NNP'), ('made', 'O', 'VB'), ('money', 'O', 'NNP'), ('.', 'O', '.'),
          ('georgia', 'B', 'NNP'), ('tech', 'I', 'NNP'), ('is', 'O', 'NNP'), ('a', 'O', '.'),
          ('university', 'O', 'NNP'), ('in', 'O', 'NNP'), ('georgia', 'B', 'NNP'), ('.', 'O', '.')]

sentences = [list(group) for k, group in groupby(output, lambda x: x[0] == ".") if not k]
result = [tuple(map(list, zip(*map(itemgetter(0, 1), sentence)))) for sentence in sentences]
print(result)

输出

[(['the', 'wall', 'street', 'journal', 'reported', 'today', 'that', 'apple', 'corporation', 'made', 'money'], ['B', 'I', 'I', 'I', 'O', 'O', 'O', 'B', 'I', 'O', 'O']), (['georgia', 'tech', 'is', 'a', 'university', 'in', 'georgia'], ['B', 'I', 'O', 'O', 'O', 'O', 'B'])]

解释

据我所知,你想把每个句子的第一个和最后一个元素拆开。你知道吗

线路:

sentences = [list(group) for k, group in groupby(output, lambda x: x[0] == ".") if not k]

output按每个.拆分成句子,第二行只是将每个句子解包:

result = [tuple(map(list, zip(*map(itemgetter(0, 1), sentence)))) for sentence in sentences]

当你想要一个列表的元组列表,zip返回一个元组列表时,你必须用list映射每个元组,然后将映射的结果转换成一个元组。你知道吗

相关问题 更多 >