python comprehension split list

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

reference = [0,0,1,0]

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

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))):
if i == 1:
temp = []
if len(sentences) == len(reference)+1:
temp = []
return result

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s