0

I have a list:

List = [[['the'], ['cool'], ['mani'], ['Party']], [','], ['has'], ['won'], [['the'], ['Delhi'], ['elections']], [','], ['with'], [['it']], ["'s"], [['leader'], ['Arv'], ['Kejjohn']], [['leading'], ['the', 'way']], ['.']]

How can i take successive elements in the list, and combine them as a single item, if they have only a single element?

Eg:- [..., [,] , [has] , [won] , [['the'],['Delhi']] ....] 

becomes:

Eg:- [..., [[,],[has],[won]] , [['the'],['Delhi']] ....]
user2878953
  • 99
  • 1
  • 5

3 Answers3

2

I've tried to solve this with generators

def f(x):
    tmp = [] # we save tmp list
    for i in x: 
        if len(i) == 1: # if there is just one element we remember it
            tmp.append(i)
        else:
            if tmp: # otherwise we return previously collected elements if they exist
                yield tmp
                tmp = [] # empty the buffer
            yield i # if element have more than 1 element we just return
    if tmp: yield tmp

a=[[','], ['has'], ['won'], [['the'], ['Delhi']]]

print list(f(a))

>> [[[','], ['has'], ['won']], [['the'], ['Delhi']]]
Community
  • 1
  • 1
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
1

If you don't like tinkering with buffers, you may use the following solution. All the heavy work is done by groupby. All I had to do is formulate the key and gather the output of groupby.

>>> from itertools import chain, groupby
>>> from pprint import pprint
>>> result = list(chain.from_iterable(([item for item in it],) if key==1 else it for key, it in y(List, key=len)))
>>> pprint(result)
[[['the'], ['cool'], ['mani'], ['Party']],
 [[','], ['has'], ['won']],
 [['the'], ['Delhi'], ['elections']],
 [[','], ['with'], [['it']], ["'s"]],
 [['leader'], ['Arv'], ['Kejjohn']],
 [['leading'], ['the', 'way']],
 [['.']]]
ovgolovin
  • 13,063
  • 6
  • 47
  • 78
0

Here's one solution. I'm using two lists, one for result, and one for joining together one-lengthed sub-lists. It's pretty self-explanatory

l = [[['the'], ['cool'], ['mani'], ['Party']], [','], ['has'], ['won'], [['the'], ['Delhi'], ['elections']], [','], ['with'], [['it']], ["'s"], [['leader'], ['Arv'], ['Kejjohn']], [['leading'], ['the', 'way']], ['.']]

res = [] # the final result
joined = [] # a temporary list for joining together elements
for sub_list in l: 
    if len(sub_list) == 1: #if there's only one element
        joined.append(sub_list) # put it in joined

    else: 
        if len(joined) > 0:  #if joined has some sub_lists
            res.append(joined) # append them to final result
            joined = [] #empty joined
        res.append(sub_list) #append the current sub_list and carry on

if len( l[-1] ) == 1: #this part accounts for edge-case of a one-lengthed last element.
    res.append(l[-1]) 
pprint(res)

output:

[[['the'], ['cool'], ['mani'], ['Party']],
 [['the'], ['Delhi'], ['elections']],
 [[','], ['has'], ['won']],
 [['leader'], ['Arv'], ['Kejjohn']],
 [[','], ['with'], [['it']], ["'s"]],
 [['leading'], ['the', 'way']],
 [['.']]]
yuvi
  • 18,155
  • 8
  • 56
  • 93