Imagine I have the following list:
>>> mylist[('a', u'DT'),('Satisfactory', u'JJ'),('tracing', u'VBG'),('with', u'IN'),('a', u'DT'),('fairly', u'RB'),('persistent', u'JJ'),('with', u'IN')]
How do I concatenate list items that fall between elements that contain u'IN'
or u'DT'
and return only the concatenated elements i.e:
[('Satisfactory tracing'),('fairly persistent')]
Here is one which shall give you the desired result. Maybe you need to optimize it a bit.
my_list = ([('a', u'DT'),('Satisfactory', u'JJ'),('tracing', u'VBG'),('with', u'IN'),('a', u'DT'),('fairly', u'RB'),('persistent', u'JJ'),('with', u'IN')])sequence_enable = False
new_list = []
for i in my_list:if i[1] == 'DT' or i[1] == 'IN':if not sequence_enable: # Start reading values sequence_enable = Truetemp_str = []else: # stop reading valuesnew_list.append(' '.join(temp_str)) sequence_enable = Falsecontinueelse: # store valuesif sequence_enable:temp_str.append(i[0])print(new_list)
# output: ['Satisfactory tracing', 'fairly persistent']