Python:重建嵌套列表

时间:2018-10-17 19:17:23

标签: python python-3.x

我有一个嵌套列表:

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

如何获得预期的格式?

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

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映射,然后将map的结果转换为元组。

答案 1 :(得分:2)

您可以使用groupby将非句点项目分组为句子,然后使用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'))
]

答案 2 :(得分:1)

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”,“ corporate”,“ made”,“ money”] ,['B','I','I','I','O','O','O','B','I','O','O']]),([ '乔治亚州','技术','是','a','大学','中','乔治亚州]],['B','I','O','O','O', 'O','B'])]