Python 在特定元素的实例之后从列表中提取相同的元素块
我试图从列表中提取连续的'NN'元素(包括'NNP'),并将其附加到在'NN'之前遇到的给定'IN'或'to'的新列表中。我怎么做Python 在特定元素的实例之后从列表中提取相同的元素块,python,list,list-comprehension,Python,List,List Comprehension,我试图从列表中提取连续的'NN'元素(包括'NNP'),并将其附加到在'NN'之前遇到的给定'IN'或'to'的新列表中。我怎么做 [['Additional', 'condition', 'of', 'DeNOx', 'activation', 'shall', 'be', 'introduced', 'in', 'order', 'to', 'provide', 'flexibility', 'and', 'robustness',
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
我尝试了以下代码。但无法捕获其他类似实例
new = ['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']
lst = []
for i,j in enumerate(new):
lst1 = []
if j == 'IN':
for i in new[i+1:]:
if 'NN' in i:
lst1.append(i)
lst.append(lst1)
break
lst = [['NNP'], ['NN']]
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
但我想改进代码以提供以下输出:
[['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
每个输出块的前面都有“IN”或“TO”
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
实际上,上面的列表(新)是该列表的基本词类:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
如何将结果映射回此列表,以便
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[['DeNOx', 'activation'], ['order'], ['NSC', 'regeneration', 'management']]
为此,您可以使用两种方便的方法:和:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
这将根据到
或中的项将初始列表分块。从除第一个块以外的每个块(为了避免任何初始NNs
),这将在元素以NN
开头时获取元素。最后,列出非真实(空)列表。您可以使用两个方便的方法:和:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
这将根据到
或
中的项将初始列表分块。从除第一个块以外的每个块(为了避免任何初始NNs
),这将在元素以NN
开头时获取元素。最后,它列出了非真实(空)列表。当我键入此内容时,发布了另一个不错的答案——这是一个没有导入的简单实现
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
full_list = []
for x in range(0, len(new)):
if 'NN' in new[x] and ('IN' in new[x-1] or 'TO' in new[x-1]):
temp_list = [new[x]]
temp_index = x+1
while 'NN' in new[temp_index]:
temp_list.append(new[temp_index])
temp_index += 1
full_list.append(temp_list)
在我打字的时候,还有一个很好的答案——这是一个没有导入的简单实现
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
full_list = []
for x in range(0, len(new)):
if 'NN' in new[x] and ('IN' in new[x-1] or 'TO' in new[x-1]):
temp_list = [new[x]]
temp_index = x+1
while 'NN' in new[temp_index]:
temp_list.append(new[temp_index])
temp_index += 1
full_list.append(temp_list)
你的车不太远。使之更容易的一种方法是获取'IN'
和'to'
的所有索引:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
starts = {'IN', 'TO'}
in_twos = [i for i, e in enumerate(new) if e in starts]
其中:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[2, 8, 10, 15]
然后您只需要迭代这些索引,特别是new[i+1://code>,并获取'NN'
或'NNP'
元素。当您到达的元素不是这些元素之一时,将
从循环中断开
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
以下是一个例子:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
result = []
take = {'NN', 'NNP'}
for i in in_twos:
temp = []
for x in new[i+1:]:
if x not in take:
break
temp.append(x)
# If this is empty, don't add it
if temp:
result.append(temp)
print(result)
最终输出:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']]
另一个较短的方法,如@schwobasegll所建议的,是使用它来简化'NN'
元素的提取。该函数基本上一直提取元素,直到第一个参数谓词返回false
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
下面是它的外观:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
from itertools import takewhile
# new, take and in_twos same as before
result = [l for l in [list(takewhile(lambda x: x in take, new[i+1:])) for i in in_twos] if l]
print(result)
# [['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']]
更新:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
如果要将单词和演讲映射到一起,可以执行以下操作:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
new = [['JJ', 'NN', 'IN','NNP','NN','MD','VB','VBN','IN','NN','TO','VB','NN','CC','NN','TO','NNP','NN','NN','.'],
['Additional','condition','of','DeNOx','activation','shall','be','introduced','in', 'order','to','provide','flexibility','and','robustness', 'to','NSC','regeneration','management','.']]
starts = {'IN', 'TO'}
in_twos = [i for i, e in enumerate(new[0]) if e in starts]
speech = []
words = []
take = {'NN', 'NNP'}
for i in in_twos:
temp = []
for x, y in zip(new[0][i+1:], new[1][i+1:]):
if x not in take:
break
temp.append((x, y))
# If this is empty, don't add it
if temp:
speech.append([x for x, _ in temp])
words.append([y for _, y in temp])
print(speech)
print(words)
哪些产出:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']]
[['DeNOx', 'activation'], ['order'], ['NSC', 'regeneration', 'management']]
你的车不太远。使之更容易的一种方法是获取'IN'
和'to'
的所有索引:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
starts = {'IN', 'TO'}
in_twos = [i for i, e in enumerate(new) if e in starts]
其中:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[2, 8, 10, 15]
然后您只需要迭代这些索引,特别是new[i+1://code>,并获取'NN'
或'NNP'
元素。当您到达的元素不是这些元素之一时,将
从循环中断开
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
以下是一个例子:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
result = []
take = {'NN', 'NNP'}
for i in in_twos:
temp = []
for x in new[i+1:]:
if x not in take:
break
temp.append(x)
# If this is empty, don't add it
if temp:
result.append(temp)
print(result)
最终输出:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']]
另一个较短的方法,如@schwobasegll所建议的,是使用它来简化'NN'
元素的提取。该函数基本上一直提取元素,直到第一个参数谓词返回false
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
下面是它的外观:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
from itertools import takewhile
# new, take and in_twos same as before
result = [l for l in [list(takewhile(lambda x: x in take, new[i+1:])) for i in in_twos] if l]
print(result)
# [['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']]
更新:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
如果要将单词和演讲映射到一起,可以执行以下操作:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
new = [['JJ', 'NN', 'IN','NNP','NN','MD','VB','VBN','IN','NN','TO','VB','NN','CC','NN','TO','NNP','NN','NN','.'],
['Additional','condition','of','DeNOx','activation','shall','be','introduced','in', 'order','to','provide','flexibility','and','robustness', 'to','NSC','regeneration','management','.']]
starts = {'IN', 'TO'}
in_twos = [i for i, e in enumerate(new[0]) if e in starts]
speech = []
words = []
take = {'NN', 'NNP'}
for i in in_twos:
temp = []
for x, y in zip(new[0][i+1:], new[1][i+1:]):
if x not in take:
break
temp.append((x, y))
# If this is empty, don't add it
if temp:
speech.append([x for x, _ in temp])
words.append([y for _, y in temp])
print(speech)
print(words)
哪些产出:
[['Additional',
'condition',
'of',
'DeNOx',
'activation',
'shall',
'be',
'introduced',
'in',
'order',
'to',
'provide',
'flexibility',
'and',
'robustness',
'to',
'NSC',
'regeneration',
'management',
'.'],
['JJ',
'NN',
'IN',
'NNP',
'NN',
'MD',
'VB',
'VBN',
'IN',
'NN',
'TO',
'VB',
'NN',
'CC',
'NN',
'TO',
'NNP',
'NN',
'NN',
'.']].
[['NNP', 'NN'], ['NN'], ['NNP', 'NN', 'NN']]
[['DeNOx', 'activation'], ['order'], ['NSC', 'regeneration', 'management']]
谢谢你的回答。但我无法将结果映射到原始列表。如果有任何关于这方面的帮助,将会非常有帮助。谢谢你的回答。但我无法将结果映射到原始列表。如果有任何关于这方面的帮助,将非常有帮助。我对我的问题添加了一些更改。如果您能提供一种方法,将合成词类(“新”列表)映射到我的问题中提到的真实英语单词列表,我将非常高兴。谢谢我对我的问题做了一些修改。如果您能提供一种方法,将合成词类(“新”列表)映射到我的问题中提到的真实英语单词列表,我将非常高兴。谢谢