根据列表中的条件合并列表项

时间:2014-02-10 01:37:03

标签: python list python-2.7 merge

我有一个项目列表: 例如:

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

我想根据条件合并列表项,即合并所有项目,直到以IP开头的项目。 我想要的输出是:

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

请建议如何做到这一点。

4 个答案:

答案 0 :(得分:2)

有趣的方式:

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])

演示:

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

答案 1 :(得分:0)

如果字符串'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']

答案 2 :(得分:0)

使用发电机。

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']

答案 3 :(得分: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']