简体   繁体   English

通过多个字符将列表拆分为块 [Python]

[英]Split a list into chunks by a number of chars [Python]

I've got a list of words (domains in my case) and I need to split this list into groups, each group should contain not more than N chars (=bytes).我有一个单词列表(在我的例子中是域),我需要将此列表分成组,每个组应包含不超过 N 个字符(=字节)。 One important thing is that the last word in each group should not break in the middle.一件重要的事情是,每组中的最后一个词不应该在中间中断。 I mean there should not be situations like:我的意思是不应该有这样的情况:

google.com
yahoo.com
bin

In my code I only managed to retrieve the first group, I don't know how to make a proper loop to split the list into multiple chunks.在我的代码中,我只设法检索了第一组,我不知道如何进行适当的循环以将列表拆分为多个块。

domains = ['google.com','yahoo.com','bing.com','microsoft.com','apple.com','amazon.com']


domains = list(domains)
text = ""
chars_sent=0
max_chars=20

for each_domain in domains:
    if chars_sent > max_chars:
        chars_sent = 0
        break
    text += each_domain+"\n"
    chars_sent += len((str(each_domain)))
print(text)

Expected output:预期 output:

google.com
yahoo.com <--- 19 chars in this part

bing.com <--- 8 chars in this part

microsoft.com <--- 13 chars in this part

apple.com
amazon.com <--- 19 chars in this part

You have to check whether adding the new element will exceed the maximum before adding it to the result.在将新元素添加到结果之前,您必须检查添加新元素是否会超过最大值。

And when you hit the limit you shouldn't break out of the loop, just add a blank line to the result.当你达到限制时,你不应该跳出循环,只需在结果中添加一个空行。

domains = ['google.com','yahoo.com','bing.com','microsoft.com','apple.com','amazon.com']

text = ""
chars_sent=0
max_chars=20

for each_domain in domains:
    if chars_sent + len(each_domain) > max_chars:
        chars_sent = 0
        text += "\n"
    text += each_domain+"\n"
    chars_sent += len(each_domain)

print(text)

There's no need to use list(domain) since it's already a list, or str(each_domain) because it's already a string.没有必要使用list(domain)因为它已经是一个列表,或者str(each_domain)因为它已经是一个字符串。

You're almost there, Here's another answer.你快到了,这是另一个答案。 if you want to get your result as nested lists (might be more pratical to use than plain text).如果您想将结果作为嵌套列表(可能比纯文本更实用)。

domains = ['google.com', 'yahoo.com', 'bing.com', 'microsoft.com', 'apple.com', 'amazon.com']
max_chars = 20

chunk_size = 0
chunks = [[]]
for domain in domains:
    if chunk_size + len(domain) > max_chars:
        chunk_size = 0
        chunks.append([])
    
    chunks[-1].append(domain)
    chunk_size += len(domain)

print(chunks)

Result:结果:

[
    ['google.com', 'yahoo.com'],
    ['bing.com'],
    ['microsoft.com'],
    ['apple.com', 'amazon.com']
]

You can also use:您还可以使用:

domains = ['google.com','yahoo.com','bing.com','microsoft.com','apple.com','amazon.com']
max_chars=20
out = [""]
for d in domains:
    if len(d) + len(out[-1].strip()) > max_chars:
        out.append(f"{d}\n")
    else:
        out[-1] = f"{out[-1].strip()}\n{d}\n"

print(*out, sep="\n")

google.com
yahoo.com

bing.com

microsoft.com

apple.com
amazon.com

Demo演示

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

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