Merge list items based on condition within the list

前端 未结 4 1682
离开以前
离开以前 2021-01-03 11:38

I have a list of items : eg:

a = [\'IP 123 84\', \'apple\', \'mercury\', \'IP 543 65\', \'killer\', \'parser\', \'goat\',
     \'IP 549 54 pineapple\', \'dja         


        
相关标签:
4条回答
  • 2021-01-03 12:06

    Kind of a fun way to do it:

    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']
    
    0 讨论(0)
  • 2021-01-03 12:07

    If string 'IP' only exists at the head of some elements of a, join the list and then split it:

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

    If a is like

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

    the former solution won't work, you may insert some special sequence (which should never appear in a) to a, and then join & split it:

    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 讨论(0)
  • 2021-01-03 12:07
    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 讨论(0)
  • 2021-01-03 12:29

    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 讨论(0)
提交回复
热议问题