Merge list items based on condition in list

I have a list of items: for example:

a = ['IP 123 84', 'apple', 'mercury', 'IP 543 65', 'killer', 'parser', 'goat',
     'IP 549 54 pineapple', 'django', 'python']

I want to combine the elements of a list based on the ie condition to merge all elements to an element starting with IP. The output I want is:

a = ['IP 123 84 apple mercury', 'IP 543 65 killer parser goat',
     'IP 549 54 pineapple django python']

Please suggest how to do this.

+3
source share
4 answers

Using a generator.

def merge(x, key='IP'):
    tmp = []
    for i in x:
        if (i[0:len(key)] == key) and len(tmp):
            yield ' '.join(tmp)
            tmp = []
        tmp.append(i)
    if len(tmp):
        yield ' '.join(tmp)

a = ['IP 123 84','apple','mercury','IP 543 65','killer','parser','goat','IP 549 54 pineapple','django','python']
print list(merge(a))

['IP 123 84 apple mercury', 'IP 543 65 killer parser goat', 'IP 549 54 pineapple django python']
0
source

This is a fun way to do this:

import itertools

def predicate_grouper(li, predicate='IP'):
    indices = [i for i,x in enumerate(li) if x.startswith(predicate)]
    slices = [slice(*x) for x in itertools.zip_longest(indices,indices[1:])]
    for sli in slices:
        yield ' '.join(li[sli])

demo:

list(predicate_grouper(a))
Out[61]: 
['IP 123 84 apple mercury',
 'IP 543 65 killer parser goat',
 'IP 549 54 pineapple django python']
+2
source

"IP" a, :

In [99]: ['IP'+i for i in ''.join(a).split('IP')[1:]]
Out[99]: 
['IP 123 84applemercury',
 'IP 543 65killerparsergoat',
 'IP 549 54 pineappledjangopython']

a

a = ['IP 123 84', 'apple', 'mercury', 'IP 543 65', 'killer', 'parserIP', 'goat',
     'IP 549 54 pineapple', 'django', 'python']                    ^^^^

, ( a) a, :

In [11]: for i, v in enumerate(a):
    ...:     if v.startswith('IP'):
    ...:         a[i]='$$$'+v
    ...: ''.join(a).split('$$$')[1:]
Out[11]: 
['IP 123 84applemercury',
 'IP 543 65killerparsergoat',
 'IP 549 54 pineappledjangopython']
0
import re    
def group_IP_list(lst):
    groups = []
    word_group = []
    for list_item in lst:
        if re.search(r'^IP',list_item) and word_group:
            groups.append(' '.join(word_group)) 
        elif re.search(r'^IP',list_item):
            word_group = [list_item]
        else: 
            word_group.extend([list_item])
    groups.append(' '.join(word_group)) 
    return groups

#Usage: 
a = ['IP 123 84','apple','mercury','IP 543 65','killer','parser','goat','IP 549 54   pineapple','django','python']
print group_IP_list(a)
#Result:
['IP 123 84 apple mercury', 'IP 123 84 apple mercury killer parser goat', 'IP 123 84 apple mercury killer parser goat django python']
0

All Articles