Python 将一个字符串分成 N 个相等的部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22571259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:11:17  来源:igfitidea点击:

Split a string into N equal parts?

python

提问by Mo.

I have a string I would like to split into N equal parts.

我有一个字符串,我想分成 N 个相等的部分。

For example, imagine I had a string with length 128 and I want to split it in to 4 chunks of length 32 each; i.e., first 32 chars, then the second 32 and so on.

例如,假设我有一个长度为 128 的字符串,我想将它分成 4 个长度为 32 的块;即,前 32 个字符,然后是第二个 32 个字符,依此类推。

How can I do this?

我怎样才能做到这一点?

采纳答案by robert king

import textwrap
print textwrap.wrap("123456789", 2)
#prints ['12', '34', '56', '78', '9']

Note: be careful with whitespace etc - this may or may not be what you want.

注意:小心空格等 - 这可能是也可能不是你想要的。

"""Wrap a single paragraph of text, returning a list of wrapped lines.

    Reformat the single paragraph in 'text' so it fits in lines of no
    more than 'width' columns, and return a list of wrapped lines.  By
    default, tabs in 'text' are expanded with string.expandtabs(), and
    all other whitespace characters (including newline) are converted to
    space.  See TextWrapper class for available keyword args to customize
    wrapping behaviour.
    """

回答by robert king

You can treat a string similarly to a list in many cases. There are lots of answers here: Splitting a list of into N parts of approximately equal length

在许多情况下,您可以像对待列表一样对待字符串。这里有很多答案:Splitting a list of into N 个部分的长度大致相等

for example you could work out the chunk_size = len(my_string)/N

例如你可以计算出 chunk_size = len(my_string)/N

Then to access a chunk you can go my_string[i: i + chunk_size](and then increment iby chunk_size) - either in a for loop or in a list comprehension.

然后访问一个块,你可以去my_string[i: i + chunk_size](然后i通过 chunk_size递增) - 无论是在 for 循环中还是在列表理解中。

回答by Tim Zimmermann

You may use a simple loop:

您可以使用一个简单的循环:

parts = [your_string[i:i+n] for i in range(0, len(your_string), n)]

回答by Adam Smith

I like iterators!

我喜欢迭代器!

def chunk(in_string,num_chunks):
    chunk_size = len(in_string)//num_chunks
    if len(in_string) % num_chunks: chunk_size += 1
    iterator = iter(in_string)
    for _ in range(num_chunks):
        accumulator = list()
        for _ in range(chunk_size):
            try: accumulator.append(next(iterator))
            except StopIteration: break
        yield ''.join(accumulator)

## DEMO
>>> string = "a"*32+"b"*32+"c"*32+"d"*32
>>> list(chunk(string,4))
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccccccccccccccc', 'dddddddddddddddddddddddddddddddd']
>>> string += "e" # so it's not evenly divisible
>>> list(chunk(string,4))
['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcc', 'ccccccccccccccccccccccccccccccddd', 'ddddddddddddddddddddddddddddde']

Also demonstrably faster than textwrap.wrap, although almost certainly less "good"

也明显比 快textwrap.wrap,虽然几乎可以肯定不那么“好”

>>> timeit.timeit(lambda: list(chunk(string,4)),number=500)
0.047726927170444355
>>> timeit.timeit(lambda: textwrap.wrap(string,len(string)//4),number=500)
0.20812756575945457

And pretty easy to hack to work with any iterable (just drop the str.joinand yield accumulator unless isinstance(in_string,str))

并且很容易破解以使用任何可迭代对象(只需删除str.join和 yield 累加器,除非isinstance(in_string,str)

# after a petty hack
>>> list(chunk([1,2,3,4,5,6,7,8,9,10,11,12],4))
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

回答by Alex Thornton

Another common way of grouping elements into n-length groups:

将元素分组为 n 长度组的另一种常见方法:

>>> s = '1234567890'
>>> list(map(''.join, zip(*[iter(s)]*2)))
['12', '34', '56', '78', '90']

This method comes straight from the docs for zip().

此方法直接来自zip().

回答by Omid Raha

Recursive way:

递归方式:

def split_str(seq, chunk, skip_tail=False):
    lst = []
    if chunk <= len(seq):
        lst.extend([seq[:chunk]])
        lst.extend(split_str(seq[chunk:], chunk, skip_tail))
    elif not skip_tail and seq:
        lst.extend([seq])
    return lst

Demo:

演示:

seq = "123456789abcdefghij"

print(split_str(seq, 3))
print(split_str(seq, 3, skip_tail=True))

# ['123', '456', '789', 'abc', 'def', 'ghi', 'j']
# ['123', '456', '789', 'abc', 'def', 'ghi']