python根据字符串拆分iterable并进行相同长度的拆分

2024-06-09 05:45:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个生成器对象,如下所示:

a = ["ab", "fasj", "sfk", "sfsfg", "ab", "gwre", "ab", "werwe", "fsfj", "lke"]

使用python,如何根据“ab”的出现将其拆分为相同长度的块?你知道吗

我在找这样的东西:

b = ["ab", "fasj", "sfk", "sfsfg"]
c = ["ab", "gwre", " ", " "]
d = ["ab", "werwe", "fsfj", "lke"]

Tags: 对象abgwrefasjfsfjwerwelkesfsfg
3条回答

以下是我使用numpy.splitlist comprehension的Pythonic方法:

import numpy as np
rslt = [list(x) for x in np.split(np.array(a), [i for i, x in enumerate(a) if x=="ab"][1:])]
maxlen = max(len(x) for x in rslt)
rslt = [x+['']*(maxlen-len(x)) if len(x)<maxlen else x for x in rslt ]

说明:

In [149]: rslt = [list(x) for x in np.split(np.array(a), [i for i, x in enumerate(a) if x=="ab"][1:])]

In [150]: rslt
Out[150]: 
[['ab', 'fasj', 'sfk', 'sfsfg'],
 ['ab', 'gwre'],
 ['ab', 'werwe', 'fsfj', 'lke']]

In [151]: maxlen = max(len(x) for x in rslt)

In [152]: maxlen
Out[152]: 4

In [153]: rslt = [x+['']*(maxlen-len(x)) if len(x)<maxlen else x for x in rslt ]

In [154]: rslt
Out[154]: 
[['ab', 'fasj', 'sfk', 'sfsfg'],
 ['ab', 'gwre', '', ''],
 ['ab', 'werwe', 'fsfj', 'lke']]

这段代码不是很干净,但它可以做到这一点。一定要亲自验证一下,看看它是否正确回答了你的问题。你知道吗

listOfLists = []
newList = []
count = 0
for i in a:
    if i == "ab":
        if newList:
            listOfLists.append(newList)
        count = 0
        newList = [" "]*5
        newList[count] = i
        count += 1
    else:
        newList[count] = i
        count += 1
listOfLists.append(newList)

以下代码并不完美,但可以完成任务(如果我正确理解了您的问题):

array = ["ab", "fasj", "sfk", "sfsfg", "ab", "gwre", "ab", "werwe", "fsfj", "lke"]

def split(l):
    def internal():
        stack = []
        for v in l:
            if v == "ab":
                if stack:
                    yield stack
                stack = []

            stack.append(v)

        if stack:
            yield stack

    ret = list(internal())
    size = max(map(len, ret))

    return [x + [" "] * (size - len(x)) for x in ret]   


print(split(array))

将返回:

[
    ["ab", "fasj", "sfk", "sfsfg"],
    ["ab", "gwre", " ", " "],
    ["ab", "werwe", "fsfj", "lke"]
]

Edit:OP刚刚声明到下一个ab的最大长度是4。这将大大简化代码:

array = ["ab", "fasj", "sfk", "sfsfg", "ab", "gwre", "ab", "werwe", "fsfj", "lke"]

def split(l):
    stack = []
    for v in l:
        if v == "ab":
            if stack:
                yield stack + [" "] * (4 - len(stack))

            stack = []

        stack.append(v)

    if stack:
        yield stack + [" "] * (4 - len(stack))

print(list(split(array)))

相关问题 更多 >