The problem is as follows. I have a list of strings
lst1=['puffing','his','first','cigarette','in', 'weeks', 'in', 'weeks']
and I would like to get a string
lst2=['puffing','his','first','cigarette','in weeks', 'in weeks']
that is, to combine any occurrence of a subword ['in', 'weeks']
for reasons that are irrelevant here, where find_sub_list1
taken from here (and included in the code below):
npis = [['in', 'weeks'], ['in', 'ages']]
def find_sub_list1(sl,l):
results=[]
sll=len(sl)
for ind in (i for i,e in enumerate(l) if e==sl[0]):
if l[ind:ind+sll]==sl:
results.append((ind,ind+sll-1))
return results
def concatenator(sent, npis):
indices = []
for npi in npis:
indices_temp = find_sub_list1(npi, sent)
if indices_temp != []:
indices.extend(indices_temp)
sorted(indices, key=lambda x: x[0])
for (a,b) in indices:
diff = b - a
sent[a:b+1] = [" ".join(sent[a:b+1])]
del indices[0]
indices = [(a - diff, b - diff) for (a,b) in indices]
return sent
instead of the desired, lst2
this encoder returns:
concatenator(lst1,['in', 'weeks'])
>>['puffing','his','first','cigarette','in weeks', 'in', 'weeks']
therefore, it combines only the first occurrence. Any ideas on where the code doesn't work?