Python merges list items in a complicated way

So I have this list

l = ['abc', 'retro', '', '', 'images', 'cool', '', 'end']

and, I want to join them in such a way as:

l = ['abc retro', '', '', 'images cool', '', 'end']

I tried many methods but nothing worked. Any suggestions?

+6
source share
5 answers

You can also use itertools.groupbylist comprehension. Group in ''and out ''and join the elements from the latter using str.join. The ternary operator at the back of the understanding uses a group key to decide what to do for each group:

from itertools import groupby

l = ['abc','retro','','','images','cool','','end']

r = [j for k, g in groupby(l, lambda x: x=='') 
                          for j in (g if k else (' '.join(g),))]
print(r)
# ['abc retro', '', '', 'images cool', '', 'end']
+6
source

Here is another version using good old loops to edit the list:

In [6]: l=['abc','retro','','','images','cool','','end']

In [7]: i = 1
   ...: while i < len(l):
   ...:     while l[i-1] and l[i]:
   ...:         l[i-1] += ' ' + l[i]
   ...:         del l[i]
   ...:     i += 1
   ...:

In [8]: l
Out[8]: ['abc retro', '', '', 'images cool', '', 'end']
+4

itertools.chain itertools.groupby() bool :

In [21]: from itertools import groupby, chain

In [22]: list(chain.from_iterable([' '.join(g)] if k else g for k, g in groupby(lst, bool)))
Out[22]: ['abc retro', '', '', 'images cool', '', 'end']
+1

- -, .

l=['abc','retro','','','images','cool','','end']

result = list()
for x in range(len(l)):
    if l[x]:
        #Its not empty lets move to next until we find one or more consecutive non empty elements
        for y in range(x+1, len(l)):
            if l[y]:
                result.append(l[x]+' '+l[y])
                x = y

            else:
                x = y
                break
    else:
        result.append(l[x])

    #We are going to append last element, because we might have read an empty before
    if x == len(l)-1:
        result.append(l[x])

print result

^^ .

l=['abc','retro','','','images','are','cool','','end']

result = list()
previous = ''
while l:
    current = l.pop(0)
    if current:
        if previous:
            previous+=' '+current
        else:
            previous = current
    else:
        result.append(previous)
        result.append('')
        previous = ''

result.append(current)
print result
+1

groupby. comp, , .;) () , ( , .join ).

We use boolas a key function, so the groups are either true or false. The false-ish group is a group of empty lines, so we expand the list of results newusing the group. The true group contains only non-empty lines, so we append it and add the result to new.

from itertools import groupby

old = ['abc', 'retro', '', '', 'images', 'cool', '', 'end']

new = []
for k, g in groupby(old, key=bool):
    if k:
        new.append(' '.join(g))
    else:
        new.extend(g)

print(new)

Exit

['abc retro', '', '', 'images cool', '', 'end']
+1
source

Source: https://habr.com/ru/post/1016348/


All Articles