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

Split list into lists based on a character occurring inside of an element

在如下列表中:

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]

知道如何成功拆分吗?

首先,快速 oneliner,就 space 要求而言,这不是最佳解决方案,但它简短而有趣:

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

将列表的元素连接成一个字符串会更容易,在 '|' 字符处拆分字符串,然后在用于连接列表的内容上拆分每个元素。可能是逗号,

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

您不需要正则表达式或任何类似的东西 - 一个简单的循环和 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']]

这是我没有找到答案的类似问题的解决方案。如何将列表拆分为由成员分隔的子列表,例如字符:

l = ['r', 'g', 'b', ':',
     'D', 'E', 'A', 'D', '/',
     'B', 'E', 'E', 'F', '/',
     'C', 'A', 'F', 'E']

def split_list(thelist, delimiters):
    ''' Split a list into sub lists, depending on a delimiter.

        delimiters - item or tuple of item
    '''
    results = []
    sublist = []

    for item in thelist:
        if item in delimiters:
            results.append(sublist) # old one
            sublist = []            # new one
        else:
            sublist.append(item)

    if sublist:  # last bit
        results.append(sublist)

    return results


print(
    split_list(l, (':', '/'))
)
# => [['r', 'g', 'b'], ['D', 'E', 'A', 'D'], 
#     ['B', 'E', 'E', 'F'], 
#     ['C', 'A', 'F', 'E']]