根据元素内部出现的字符将列表拆分为列表

omr*_*hur 4 python list delimiter

在如下列表中:

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']
Run Code Online (Sandbox Code Playgroud)

可能有一些数字元素前面有一个字符.我想把它分成如下的子列表:

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']
]
Run Code Online (Sandbox Code Playgroud)

正如您所知,根据角色,列表可能看起来很相似.否则,它们可能具有不同数量的元素或完全不同的元素.主分隔符是"|"字符.我试图运行以下代码来拆分列表,但我得到的只是列表中相同的,更大的列表.即,列表len(list) == 1.

import itertools

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

有任何想法如何成功分裂?

ran*_*mir 7

首先,一个快速的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']]
Run Code Online (Sandbox Code Playgroud)

在这里,我们通过一个独特的非出现分隔符连接大列表的所有元素,例如,,然后拆分|,然后再将每个列表拆分成原始元素的子列表.

但是如果你正在寻找一个更有效的解决方案,你可以使用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]
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 5

'|'将列表的元素连接到单个字符串中,根据字符拆分字符串,然后根据用于连接列表的元素拆分每个元素会更容易。可能是逗号,

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']]
Run Code Online (Sandbox Code Playgroud)

如果列表很长,您还可以迭代列表中的项目,在遇到管道字符时创建新的子列表|

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', '']]
Run Code Online (Sandbox Code Playgroud)