如何使用 python 定义函数来集成列表?
How I can define function to integrate lists using python?
我正在尝试定义一个遍历三种列表列表的函数,每个列表具有以下结构:
sentiment_labels = [['B_S', 'O', 'O', 'O'], ['O', 'O', 'B_S', 'O']]
aspect_labels = [['O', 'B_A', 'O', 'O'], ['O', 'O', 'O', 'B_A']]
modifier_labels = [['O', 'O', 'BM', 'O'], ['O', 'O', 'O', 'O']]
# those lists contain 'B_A', 'I_S', 'B_S', 'I_S', 'BM', 'IM', and 'O' labels (IOB Tagging)
目标结果必须像:
labels = [['B_S', 'B_A', 'BM', 'O'], ['O', 'O', 'B_S', 'B_A'] ]
为此,我定义了以下函数:
# define function to integrate sentiments, aspects, and modifiers lists
def integrate_labels(sentiments_lists, aspects_lists, modifiers_lists):
all_labels = []
integrated_label = []
# iterate through each list, then iterate through each element
for sentiment_list, aspect_list, modifier_list in zip(sentiments_lists, aspects_lists, modifiers_lists):
for sentiment, aspect, modifier in zip(sentiment_list, aspect_list, modifier_list):
# if the element is a sentiment label append it to the integrated_label list
if sentiment != 'O':
integrated_label.append(sentiment)
# if the element is an aspect label append it to the integrated_label list
elif aspect != 'O':
integrated_label.append(aspect)
# if the element is a modifier label append it to the integrated_label list
elif modifier != 'O':
integrated_label.append(modifier)
else:
integrated_label.append('O')
# now append each integrated_label list to all_labels list
all_labels.append(integrated_label)
return all_labels
但我得到了这个结果:
integrate_labels(sentiment_labels, aspect_labels, modifier_labels)
[['B_S', 'B_A', 'BM', 'O', 'O', 'O', 'B_S', 'B_A'],
['B_S', 'B_A', 'BM', 'O', 'O', 'O', 'B_S', 'B_A']]
如何更改 integrate_labels 函数以获得 目标结果?
提前致谢!
改为
# now append each integrated_label list to all_labels list
all_labels.append(integrated_label.copy())
integrated_label = []
我正在尝试定义一个遍历三种列表列表的函数,每个列表具有以下结构:
sentiment_labels = [['B_S', 'O', 'O', 'O'], ['O', 'O', 'B_S', 'O']]
aspect_labels = [['O', 'B_A', 'O', 'O'], ['O', 'O', 'O', 'B_A']]
modifier_labels = [['O', 'O', 'BM', 'O'], ['O', 'O', 'O', 'O']]
# those lists contain 'B_A', 'I_S', 'B_S', 'I_S', 'BM', 'IM', and 'O' labels (IOB Tagging)
目标结果必须像:
labels = [['B_S', 'B_A', 'BM', 'O'], ['O', 'O', 'B_S', 'B_A'] ]
为此,我定义了以下函数:
# define function to integrate sentiments, aspects, and modifiers lists
def integrate_labels(sentiments_lists, aspects_lists, modifiers_lists):
all_labels = []
integrated_label = []
# iterate through each list, then iterate through each element
for sentiment_list, aspect_list, modifier_list in zip(sentiments_lists, aspects_lists, modifiers_lists):
for sentiment, aspect, modifier in zip(sentiment_list, aspect_list, modifier_list):
# if the element is a sentiment label append it to the integrated_label list
if sentiment != 'O':
integrated_label.append(sentiment)
# if the element is an aspect label append it to the integrated_label list
elif aspect != 'O':
integrated_label.append(aspect)
# if the element is a modifier label append it to the integrated_label list
elif modifier != 'O':
integrated_label.append(modifier)
else:
integrated_label.append('O')
# now append each integrated_label list to all_labels list
all_labels.append(integrated_label)
return all_labels
但我得到了这个结果:
integrate_labels(sentiment_labels, aspect_labels, modifier_labels)
[['B_S', 'B_A', 'BM', 'O', 'O', 'O', 'B_S', 'B_A'],
['B_S', 'B_A', 'BM', 'O', 'O', 'O', 'B_S', 'B_A']]
如何更改 integrate_labels 函数以获得 目标结果?
提前致谢!
改为
# now append each integrated_label list to all_labels list
all_labels.append(integrated_label.copy())
integrated_label = []