2017-07-24 62 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']]