2017-07-24 150 views
1

的内部发生在像下面的一个列表的字符:拆分列表成列表基于一个元件

biglist = ['X', '1498393178', '1|Y', '15496686585007', '-82', '-80', '-80', '3', '3', '2', '|Y', '145292534176372', '-87', '-85', '-85', '3', '3', '2', '|Y', '11098646289856', '-91', '-88', '-89', '3', '3', '2', '|Y', '35521515162112', '-82', '-74', '-79', '3', '3', '2', '|Z', '0.0', '0.0', '0', '0', '0', '0', '0', '4', '0', '154'] 

有可能是由一个字符之前一些数字元素。我想闯入子列表此象下面这样:

smallerlist = [ 
['X', '1498393', '1'], 
['Y', '1549668', '-82', '-80', '-80', '3', '3', '2', ''], 
['Y', '1452925', '-87', '-85', '-85', '3', '3', '2', ''], 
['Y', '3552151', '-82', '-74', '-79', '3', '3', '2', ''], 
['Z', '0.0', '0.0', '0', '0', '0', '0', '0', '4', '0', '154'] 
] 

正如你所知道的,根据性质,名单可能看起来类似。否则,他们可能有不同数量的元素,或者完全不同的元素。主分隔符是"|"字符。我试图运行下面的代码来分割列表,但我得到的只是列表中的同一个更大的列表。即,列表len(list) == 1

import itertools 

delim = '|' 
smallerlist = [list(y) for x, y in itertools.groupby(biglist, lambda z: z == delim) if not x] 

任何想法如何成功地分裂它?

回答

6

首先,快速oneliner,这是不是在空间方面要求的最佳解决方案,但它的短期和甜:

>>> smallerlist = [l.split(',') for l in ','.join(biglist).split('|')] 
>>> smallerlist 
[['X', '1498393178', '1'], 
['Y', '15496686585007', '-82', '-80', '-80', '3', '3', '2', ''], 
['Y', '145292534176372', '-87', '-85', '-85', '3', '3', '2', ''], 
['Y', '11098646289856', '-91', '-88', '-89', '3', '3', '2', ''], 
['Y', '35521515162112', '-82', '-74', '-79', '3', '3', '2', ''], 
['Z', '0.0', '0.0', '0', '0', '0', '0', '0', '4', '0', '154']] 

下面我们通过一个独特的非加入大名单中的所有元素 - 出现的分隔符,例如,,然后拆分|,然后再将每个列表拆分为原始元素的子列表。

但是,如果你正在寻找一个有点有效的解决方案,你可以用itertools.groupby,将中间列表进行操作,在与breakby()发生器,在不|分离元件返回实时生成做是和那些分隔符分为3个元素:第一部分,列表分隔符(例如None)和第二部分。

from itertools import groupby 

def breakby(biglist, sep, delim=None): 
    for item in biglist: 
     p = item.split(sep) 
     yield p[0] 
     if len(p) > 1: 
      yield delim 
      yield p[1] 

smallerlist = [list(g) for k,g in groupby(breakby(biglist, '|', None), 
              lambda x: x is not None) if k] 
2

它会更容易在列表的元素加入到一个单一的字符串,分裂的'|'字符的字符串,然后分割每一元素上使用您加入该列表的内容。可能是一个逗号,

bigstr = ','.join(biglist) 

[line.split(',') for line in bigstr.split('|')] 

# returns 
[['X', '1498393178', '1'], 
['Y', '15496686585007', '-82', '-80', '-80', '3', '3', '2', ''], 
['Y', '145292534176372', '-87', '-85', '-85', '3', '3', '2', ''], 
['Y', '11098646289856', '-91', '-88', '-89', '3', '3', '2', ''], 
['Y', '35521515162112', '-82', '-74', '-79', '3', '3', '2', ''], 
['Z', '0.0', '0.0', '0', '0', '0', '0', '0', '4', '0', '154']] 

如果列表很长,你还可以重复该列表中的项目,当你遇到一个竖线|

new_biglist = [] 
sub_list = [] 
for item in biglist: 
    if '|' in item: 
     end, start = item.split('|') 
     sub_list.append(end) 
     new_biglist.append(sub_list) 
     sub_list = [start] 
    else: 
     sub_list.append(item) 

new_biglist 
# return: 
[['X', '1498393178', '1'], 
['Y', '15496686585007', '-82', '-80', '-80', '3', '3', '2', ''], 
['Y', '145292534176372', '-87', '-85', '-85', '3', '3', '2', ''], 
['Y', '11098646289856', '-91', '-88', '-89', '3', '3', '2', ''], 
['Y', '35521515162112', '-82', '-74', '-79', '3', '3', '2', '']] 
+0

这也是一个非常好的解决方案,我试过了,它可以工作。对于你编辑的部分,它会为'start'引发一个'NameError' – omrakhur

0

你不上创建一个新的子列表不需要正则表达式或类似的东西 - 一个简单的循环和str.split()应该是绰绰有余的,至少如果你是在一个实际有效的解决方案之后:

biglist = ['X', '1498393178', '1|Y', '15496686585007', '-82', '-80', '-80', '3', '3', '2', 
      '|Y', '145292534176372', '-87', '-85', '-85', '3', '3', '2', '|Y', 
      '11098646289856', '-91', '-88', '-89', '3', '3', '2', '|Y', '35521515162112', 
      '-82', '-74', '-79', '3', '3', '2', '|Z', '0.0', '0.0', '0', '0', '0', '0', 
      '0', '4', '0', '154'] 

delimiter = "|" 
smaller_list = [[]] 
for x in biglist: 
    if delimiter in x: 
     a, b = x.split(delimiter) 
     if a: # remove the check if you also want the empty elements 
      smaller_list[-1].append(a) 
     smaller_list.append([]) 
     if b: # remove the check if you also want the empty elements 
      smaller_list[-1].append(b) 
    else: 
     smaller_list[-1].append(x) 

print(smaller_list) 
# [['X', '1498393178', '1'], 
# ['Y', '15496686585007', '-82', '-80', '-80', '3', '3', '2'], 
# ['Y', '145292534176372', '-87', '-85', '-85', '3', '3', '2'], 
# ['Y', '11098646289856', '-91', '-88', '-89', '3', '3', '2'], 
# ['Y', '35521515162112', '-82', '-74', '-79', '3', '3', '2'], 
# ['Z', '0.0', '0.0', '0', '0', '0', '0', '0', '4', '0', '154']]