简体   繁体   English

如何将字符串列表拆分为len(“”。join(sublist))<= N的子列表?

[英]How to split list of strings into sublists where len(“”.join(sublist)) <= N?

I'm given a list of strings and a length limit N . 我得到了一个字符串列表和一个长度限制N I have to write a function to accumulate consecutive strings in the list until the next string would exceed the N limit. 我必须编写一个函数来累积列表中的连续字符串,直到下一个字符串超过N限制为止。 I have to return a list of lists, where each list is the largest consecutive substring not to exceed N characters total. 我必须返回一个列表列表,其中每个列表是最大的连续子字符串,总和不超过N字符。 See the test case below for an example. 有关示例,请参见下面的测试用例。 Also, if any single string in the list is longer than N , I have to print a useful message and return. 另外,如果列表中的任何单个字符串长于N ,我必须打印一条有用的消息并返回。

def break_lst(lst, size):
    def len_lst(l):
        return len("".join(l))

    result = []
    sublst = []

    for i, v in enumerate(lst):
        sublst.append(v)
        ls = len_lst(sublst)

        if ls == size:
            result.append(sublst)
            sublst = []
        elif ls > size:
            prev_sublst = sublst[:-1]
            if not prev_sublst or len_lst(prev_sublst) > size:
                raise Exception("Error: use a bigger size than " + str(size))
            else:
                result.append(prev_sublst)
                sublst = []

    return result

if __name__ == "__main__":
    lst = ["1", "22", "333", "4444", "55555", "666666", "7777777", "88888888"]

    for i in range(17):
        try:
            print(i, break_lst(lst, size=i))
        except Exception as e:
            print(e)

The above code it's not only ugly but also buggy, it's giving me this output: 上面的代码不仅丑陋,而且有错误,给了我以下输出:

Error: use a bigger size than 0
Error: use a bigger size than 1
Error: use a bigger size than 2
Error: use a bigger size than 3
Error: use a bigger size than 4
Error: use a bigger size than 5
Error: use a bigger size than 6
Error: use a bigger size than 7
8 [['1', '22', '333'], ['55555'], ['7777777']]
9 [['1', '22', '333'], ['55555'], ['7777777']]
10 [['1', '22', '333', '4444'], ['55555'], ['7777777']]
11 [['1', '22', '333', '4444'], ['666666']]
12 [['1', '22', '333', '4444'], ['666666']]
13 [['1', '22', '333', '4444'], ['666666', '7777777']]
14 [['1', '22', '333', '4444'], ['666666', '7777777']]
15 [['1', '22', '333', '4444', '55555'], ['666666', '7777777']]
16 [['1', '22', '333', '4444', '55555']]

When the expected output should be: 预期输出应为:

Error: use a bigger size than 0
Error: use a bigger size than 1
Error: use a bigger size than 2
Error: use a bigger size than 3
Error: use a bigger size than 4
Error: use a bigger size than 5
Error: use a bigger size than 6
Error: use a bigger size than 7
8 [['1', '22', '333'], ['4444'], ['55555'], ['666666'], ['7777777'], ['88888888']]
9 [['1', '22', '333'], ['4444', '55555'], ['666666'], ['7777777'], ['88888888']]
10 [['1', '22', '333', '4444'], ['55555'], ['666666'], ['7777777'], ['88888888']]
11 [['1', '22', '333', '4444'], ['55555', '666666'], ['7777777'], ['88888888']]
12 [['1', '22', '333', '4444'], ['55555', '666666'], ['7777777'], ['88888888']]
13 [['1', '22', '333', '4444'], ['55555', '666666'], ['7777777'], ['88888888']]
14 [['1', '22', '333', '4444'], ['55555', '666666'], ['7777777'], ['88888888']]
15 [['1', '22', '333', '4444', '55555'], ['666666', '7777777'], ['88888888']]
16 [['1', '22', '333', '4444', '55555'], ['666666', '7777777'], ['88888888']]

Any suggestion? 有什么建议吗?

The problem is in this section: 问题在此部分中:

    elif ls > size:
        prev_sublst = sublst[:-1]
        if not prev_sublst or len_lst(prev_sublst) > size:
            raise Exception("Error: use a bigger size than " + str(size))
        else:
            result.append(prev_sublst)
            sublst = []

You've determined that the next list exceeds the given length. 您确定下一个列表超出了给定的长度。 You've properly backed up one element and recorded the maxed-out sublist. 您已经正确备份了一个元素并记录了已最大化的子列表。 However, you have not found a home for the v element that just blew your size limit. 但是,您还没有找到v元素的家,它只是超出了您的大小限制。

For starters, try putting it into your new sublist: 对于初学者,请尝试将其放入新的子列表中:

        else:
            result.append(prev_sublst)
            sublst = [v]     # <=== the change is here.

Now, check your logic -- you'll want to validate this one-item list before you iterate to the next one. 现在,检查您的逻辑-您需要在迭代到下一个清单之前验证此一个清单。

One small comment on 一则评论

for i, v in enumerate(lst):

Why did you go to the trouble of enumerate when you never use i ? 当您从不使用i时,为什么要麻烦enumerate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM