如何将这些重复的代码转换成一个函数



我有一点重复的代码。我试着检查句子列表中的每个句子是否在开头包含某个单词。如果没有,我希望它做点什么,然后将值打印到字典中。否则,我想让它做别的事情然后打印另一个字典。

但是我不知道如何将有问题的重复代码转换为函数,因为我需要在重复代码中的第一个循环开始时使用变量。

下面是目前为止的代码:
if not audio_keyword_dictionary:
for index, sentence in enumerate(sentences):
keyword_set_inside_dict = set()
for each_keyword in complete_keyword_list:
if each_keyword in sentence:
keyword_set_inside_dict.add(each_keyword)
keyword_dict[sentence] = keyword_set_inside_dict
for key, value in keyword_dict.items():
print(f'{value} - {key}n')
else:
for index, sentence in enumerate(sentences):
keyword_set_inside_dict = set()
for each_keyword in complete_keyword_list:
if each_keyword in sentence:
keyword_set_inside_dict.add(each_keyword)
keyword_dict[sentence] = keyword_set_inside_dict
for each_value in audio_keyword_dictionary.values():
if each_value in sentences[index - 1] and sentences[index - 1] != '':
audio_nested_dictionary[each_value] = keyword_dict
for k, v in audio_nested_dictionary.items():
for key, value in v.items():
print(f'{k} - {value} {key}n')
下面是我创建的函数:
def iterate_sentences(sentences, keyword_list):
keyword_dict = dict()
for index, sentence in enumerate(sentences):
keyword_list_inside_dict = list()
for each_keyword in complete_keyword_list:
if each_keyword in sentence:
keyword_list_inside_dict.append(each_keyword)
keyword_set = set(keyword_list_inside_dict)
keyword_dict[sentence] = keyword_set
return keyword_dict

但是,如果我需要在重复部分之外的'index',我该如何使用它?

在这部分中,准确地说,就在重复部分结束后:

for each_value in audio_keyword_dictionary.values():
if each_value in sentences[index - 1] and sentences[index - 1] != '':
audio_nested_dictionary[each_value] = keyword_dict

不确定是否需要说,但句子是一个遵循模式的句子列表。它以包含audio{positive_number_here}的字符串开始,后面跟着另一个具有实际意义的字符串,然后是另一个audio{positive_number_here + 1},以此类推,在两者之间交替。列表以一个有意义的字符串结束。

您不需要一个函数来消除重复。只需移动if语句

for index, sentence in enumerate(sentences):
keyword_set_inside_dict = set()
for each_keyword in complete_keyword_list:
if each_keyword in sentence:
keyword_set_inside_dict.add(each_keyword)
keyword_dict[sentence] = keyword_set_inside_dict
if audio_keyword_dictionary:
for each_value in audio_keyword_dictionary.values():
if each_value in sentences[index - 1] and sentences[index - 1] != '':
audio_nested_dictionary[each_value] = keyword_dict
if audio_keyword_dictionary:
for k, v in audio_nested_dictionary.items():
for key, value in v.items():
print(f'{k} - {value} {key}n')
else:
for key, value in keyword_dict.items():
print(f'{value} - {key}n')

几个选项:

  1. 创建一个类并将您的函数添加到该类中。这样,您可以设置self.index,并在以后参考。
class SentenceManager:
def iterate_sentences(self, sentences, keyword_list):
keyword_dict = dict()
for index, sentence in enumerate(sentences):
self.index = index # set index
keyword_list_inside_dict = list()
for each_keyword in complete_keyword_list:
if each_keyword in sentence:
keyword_list_inside_dict.append(each_keyword)
keyword_set = set(keyword_list_inside_dict)
keyword_dict[sentence] = keyword_set
return keyword_dict
manager = SentenceManager(...)
manager.interate_sentences()
  1. 从函数返回一个元组,如何使用它见下文。
def iterate_sentences(sentences, keyword_list):
keyword_dict = dict()
for index, sentence in enumerate(sentences):
keyword_list_inside_dict = list()
for each_keyword in complete_keyword_list:
if each_keyword in sentence:
keyword_list_inside_dict.append(each_keyword)
keyword_set = set(keyword_list_inside_dict)
keyword_dict[sentence] = keyword_set
return keyword_dict, index # return a tuple as (keyword_dict, index)
keyword_dict, index = iterate_sentences(...) # consume tuple

相关内容

  • 没有找到相关文章

最新更新