I have recently finished a master thesis about genetic algorithms for text segmentation via python, so I have a complicated relationship with python. Actually it always helps to me. This post consists of some python especially list and dictionary comprehensions.
First of all, splitting a list regarding with given another list. For example, you have a list which has sentences as elements. Another list is reference splitter and consists of 0s and 1s.sentences = ["first sentence", "second sentence", "third sentence", "fourth sentence", "fifth sentence"]This list indicates that cut the number of index of '1' at the gap in the sentences list and result list will include two sublists
reference = [0,0,1,0]
result = [["first sentence", "second sentence", "third sentence"], ["fourth sentence", "fifth sentence"]]
def split_segments_according_to_reference(reference, sentences): temp = [] result = [] for i, j in zip(reference,range(len(sentences))): temp.append(sentences[j]) if i == 1: result.append(temp) temp = [] if len(sentences) == len(reference)+1: temp.append(sentences[-1]) result.append(temp) temp = [] return result