如何将字符串拆分为列表中的字符串数量的位置?

发布于 2025-01-24 09:30:11 字数 172 浏览 0 评论 0原文

因此,如果我有一个字符串:

s = "this is just a sample string"

我想获得每个3个字符的列表:

l = ["thi", "s i", "s j", "ust", " a ", ...]

So if I have a string:

s = "this is just a sample string"

I want to obtain a list of 3 characters each:

l = ["thi", "s i", "s j", "ust", " a ", ...]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜夜流光相皎洁 2025-01-31 09:30:11

不要将list用于变量名称,因为它是Python中的关键字。您可以做到这一点:

string =  "this is just a sample string"
l = [string[i:i+3] for i in range(0,len(string),3)]
print(l)

输出:

['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']

Don't use list for a variable name because it's a keyword in Python. Here's how you can do it:

string =  "this is just a sample string"
l = [string[i:i+3] for i in range(0,len(string),3)]
print(l)

Output:

['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']
栩栩如生 2025-01-31 09:30:11

您可以使用list Gracemension

string = "this is just a sample string"
n = 3
[string[i:i+n] for i in range(0, len(string), n)]

输出

chunks = ['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']

you can use list comprehension

string = "this is just a sample string"
n = 3
[string[i:i+n] for i in range(0, len(string), n)]

output

chunks = ['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']
ㄟ。诗瑗 2025-01-31 09:30:11

使用 more-itertools

from more_itertools import chunked
list = [''.join(chunk) for chunk in chunked(string, 3)]

With more-itertools:

from more_itertools import chunked
list = [''.join(chunk) for chunk in chunked(string, 3)]
装迷糊 2025-01-31 09:30:11

您可以使用DOT匹配1-3个字符,以匹配任何字符,包括空间和量词{1,3}

import re

print(re.findall(r".{1,3}", "this is just a sample string"))

输出,

['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']

如果您不希望'g'的单个字符匹配然后您可以使用。{3}而不是{1,3}

You can match 1-3 characters using the dot to match any character including a space and a quantifier {1,3}

import re

print(re.findall(r".{1,3}", "this is just a sample string"))

Output

['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']

If you don't want the single char match for 'g' then you can use .{3} instead of {1,3}

笑,眼淚并存 2025-01-31 09:30:11

使用发电机将字符串拆分为固定大小的块。如果字符串的长度不是块大小的倍数,则将添加“尾巴”(没有提供信息)。如果不需要“尾巴”,请检查len(string)%block_size == 0:如果false false 然后output [: - 1]

import itertools as it

s = "this is just a sample string"

s = it.tee(s, 1)[0] # as generator, or just iter(s)!
out = []
for block in iter(lambda: ''.join(it.islice(s, 0, 3, None)), ''):
    out.append(block)

print(out)

或带有循环

import itertools as it

s = "this is just a sample string"

s_iter = iter(lambda: ''.join(it.islice(s, 0, 3, None)), '')
out = []
v = is_over = next(s_iter, False)
while is_over is not False:
    out.append(v)
    v = is_over = next(s_iter, False)

print(out)

Using generators to split the string in fixed-size blocks. If the length of the string is not a multiple of the block's size then the "tail" it will be also be added (no information provided). If "tail" not desired check if len(string) % block_size == 0: if False then output[:-1].

import itertools as it

s = "this is just a sample string"

s = it.tee(s, 1)[0] # as generator, or just iter(s)!
out = []
for block in iter(lambda: ''.join(it.islice(s, 0, 3, None)), ''):
    out.append(block)

print(out)

or with a while loop

import itertools as it

s = "this is just a sample string"

s_iter = iter(lambda: ''.join(it.islice(s, 0, 3, None)), '')
out = []
v = is_over = next(s_iter, False)
while is_over is not False:
    out.append(v)
    v = is_over = next(s_iter, False)

print(out)
一杆小烟枪 2025-01-31 09:30:11

这是另一个解决方案:

s = "this is just a sample string"
length =  3
lst = []
i = 0
while i < len(s):
    lst.append(s[i:i+length])
    i+=length
print(lst)

输出:

['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']

Here is another solution:

s = "this is just a sample string"
length =  3
lst = []
i = 0
while i < len(s):
    lst.append(s[i:i+length])
    i+=length
print(lst)

Output:

['thi', 's i', 's j', 'ust', ' a ', 'sam', 'ple', ' st', 'rin', 'g']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文