如何每 3 个索引对字符串进行切片?

发布于 2024-11-02 01:56:00 字数 328 浏览 2 评论 0原文

我正在使用 Python 为我工作的实验室编程。如何切出给定字符串中的每 3 个字符并将其附加到列表中?

即 XXXxxxXXXxxxXXXxxxXXXxxxXXX (其中 X 或 x 是任何给定的字母)

string = 'XXXxxxXXXxxxXXXxxxXXXxxxXXX'
mylist = []

for x in string:
    string[?:?:?]
    mylist.append(string)

我希望列表如下所示: ['XXX','xxx','XXX','xxx','XXX'....等]

有什么想法吗?

I'm using Python to program for the lab I work at. How can I slice out every 3 characters in a given string and append it to a list?

i.e. XXXxxxXXXxxxXXXxxxXXXxxxXXX (where X or x is any given letter)

string = 'XXXxxxXXXxxxXXXxxxXXXxxxXXX'
mylist = []

for x in string:
    string[?:?:?]
    mylist.append(string)

I want the list to look like this: ['XXX','xxx','XXX','xxx','XXX'....etc]

Any ideas?

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

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

发布评论

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

评论(4

你与昨日 2024-11-09 01:56:00

简而言之,你不能。

更长的时间,您可能需要编写自己的函数:

def split(str, num):
    return [ str[start:start+num] for start in range(0, len(str), num) ]

例如:

>>> split("xxxXXX", 3)
['xxx', 'XXX']
>>> split("xxxXXXxx", 3)
['xxx', 'XXX', 'xx']

In short, you can't.

In longer, you'll need to write your own function, possibly:

def split(str, num):
    return [ str[start:start+num] for start in range(0, len(str), num) ]

For example:

>>> split("xxxXXX", 3)
['xxx', 'XXX']
>>> split("xxxXXXxx", 3)
['xxx', 'XXX', 'xx']
萌︼了一个春 2024-11-09 01:56:00

将列表拆分为 3 个块和将字符串拆分为 3 个块之间的一个区别是 re 模块适用于字符串而不是列表。

如果性能很重要(即您要分割数千个字符串),您应该测试如何在应用程序中比较各种答案。

>>> import re
>>> re.findall('...','XXXxxxXXXxxxXXXxxxXXXxxxXXX')
['XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX']

>>> chunksize=3
>>> re.findall('.{%s}'%chunksize,'XXXxxxXXXxxxXXXxxxXXXxxxXXX')
['XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX']

这之所以有效,是因为 . 表示正则表达式中的“匹配任何字符”。
.{3}​​ 表示“匹配任意3个字符”,依此类推

one difference between splitting lists into chunks of 3 and strings into chunks of 3 is that the re module works with strings rather than lists.

If performance is important (ie you are splitting thousands of strings), you should test how the various answers compare in your application

>>> import re
>>> re.findall('...','XXXxxxXXXxxxXXXxxxXXXxxxXXX')
['XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX']

>>> chunksize=3
>>> re.findall('.{%s}'%chunksize,'XXXxxxXXXxxxXXXxxxXXXxxxXXX')
['XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX', 'xxx', 'XXX']

This works because . means "match any character" in regular expressions.
.{3} means "match any 3 characters", and so on

自此以后,行同陌路 2024-11-09 01:56:00

据我所知,没有内置方法可以让您对每个 x 索引对 str 进行分块。然而这应该有效:

 str = "stringStringStringString"

 def chunk_str(str, chunk_size):
   return [str[i:i+chunk_size] for i in range(0, len(str), chunk_size)]

 chunk_str(str,3)

产生:

['str', 'ing', 'Str', 'ing', 'Str', 'ing', 'Str', 'ing']

As far as I know there is no built in method that allows you to chunk an str every x indices. However this should works:

 str = "stringStringStringString"

 def chunk_str(str, chunk_size):
   return [str[i:i+chunk_size] for i in range(0, len(str), chunk_size)]

 chunk_str(str,3)

produces:

['str', 'ing', 'Str', 'ing', 'Str', 'ing', 'Str', 'ing']
二智少女 2024-11-09 01:56:00

从 2008 年 11 月起复制 How do you split a list into equal-sized chunks in Python? 的答案:

直接来自 Python 文档(食谱for itertools):

from itertools import izip, chain, repeat

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

另一种方式,正如 JFSebastian 所建议的:

from itertools import izip_longest

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip_longest(*[iter(iterable)]*n, fillvalue=padvalue)

我猜 Guido 的时间机器可以工作——工作——将工作——将工作——再次工作。

Copying an answer from How do you split a list into evenly sized chunks in Python? since Nov 2008:

Directly from the Python documentation (recipes for itertools):

from itertools import izip, chain, repeat

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

An alternate take, as suggested by J.F.Sebastian:

from itertools import izip_longest

def grouper(n, iterable, padvalue=None):
    "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
    return izip_longest(*[iter(iterable)]*n, fillvalue=padvalue)

I guess Guido's time machine works—worked—will work—will have worked—was working again.

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