如何用python分割这个字符串?

发布于 2024-09-27 10:24:31 字数 225 浏览 7 评论 0原文

我的字符串类似于此示例: “AAABBBCDEEEEBBBAA”

字符串中可以包含任何字符。

我想将其拆分为一个列表,例如: ['AAA','BBB','C','D','EEEE','BBB','AA']

因此相同字符的每个连续延伸都会进入拆分列表的单独元素。

我知道我可以迭代字符串中的字符,检查每个 i 和 i-1 对是否包含相同的字符等。但是有没有更简单的解决方案?

I have strings that look like this example:
"AAABBBCDEEEEBBBAA"

Any character is possible in the string.

I want to split it to a list like:
['AAA','BBB','C','D','EEEE','BBB','AA']

so every continuous stretch of the same characters goes to separate element of the split list.

I know that I can iterate over characters in the string, check every i and i-1 pair if they contain the same character, etc. but is there a more simple solution out there?

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

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

发布评论

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

评论(4

原谅我要高飞 2024-10-04 10:24:32

只是解决问题的另一种方法:

#!/usr/bin/python

string = 'AAABBBCDEEEEBBBAA'
memory = str()
List = list()
for index, element in enumerate(string):
    if index > 0:
        if string[index] == string[index - 1]:
            memory += string[index]
        else:
            List.append(memory)
            memory = element
    else:
        memory += element

print List

Just another way of soloving your problem :

#!/usr/bin/python

string = 'AAABBBCDEEEEBBBAA'
memory = str()
List = list()
for index, element in enumerate(string):
    if index > 0:
        if string[index] == string[index - 1]:
            memory += string[index]
        else:
            List.append(memory)
            memory = element
    else:
        memory += element

print List
岁月染过的梦 2024-10-04 10:24:31

我们可以使用正则表达式:

>>> import re
>>> r = re.compile(r'(.)\1*')
>>> [m.group() for m in r.finditer('AAABBBCDEEEEBBBAA')]
['AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']

或者,我们可以使用 itertools.groupby

>>> import itertools
>>> [''.join(g) for k, g in itertools.groupby('AAABBBCDEEEEBBBAA')]
['AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']

timeit 显示正则表达式更快(对于这个特定字符串)(Python 2.6Python 3.1)。但Regex毕竟是字符串专用的,而groupby是一个通用函数,所以这并不意外。

We could use Regex:

>>> import re
>>> r = re.compile(r'(.)\1*')
>>> [m.group() for m in r.finditer('AAABBBCDEEEEBBBAA')]
['AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']

Alternatively, we could use itertools.groupby.

>>> import itertools
>>> [''.join(g) for k, g in itertools.groupby('AAABBBCDEEEEBBBAA')]
['AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']

timeit shows Regex is faster (for this particular string) (Python 2.6, Python 3.1). But Regex is after all specialized for string, and groupby is a generic function, so this is not so unexpected.

¢蛋碎的人ぎ生 2024-10-04 10:24:31
>>> from itertools import groupby
>>> [''.join(g) for k, g in groupby('AAAABBBCCD')]
['AAAA', 'BBB', 'CC', 'D']

并通过正常的字符串操作

>>> a=[];S="";p=""
>>> s
'AAABBBCDEEEEBBBAA'
>>> for c in s:
...     if c != p: a.append(S);S=""
...     S=S+c
...     p=c
...
>>> a.append(S)
>>> a
['', 'AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']
>>> filter(None,a)
['AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']
>>> from itertools import groupby
>>> [''.join(g) for k, g in groupby('AAAABBBCCD')]
['AAAA', 'BBB', 'CC', 'D']

And by normal string manipulation

>>> a=[];S="";p=""
>>> s
'AAABBBCDEEEEBBBAA'
>>> for c in s:
...     if c != p: a.append(S);S=""
...     S=S+c
...     p=c
...
>>> a.append(S)
>>> a
['', 'AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']
>>> filter(None,a)
['AAA', 'BBB', 'C', 'D', 'EEEE', 'BBB', 'AA']
穿透光 2024-10-04 10:24:31
import itertools
s = "AAABBBCDEEEEBBBAA"
["".join(chars) for _, chars in itertools.groupby(s)]
import itertools
s = "AAABBBCDEEEEBBBAA"
["".join(chars) for _, chars in itertools.groupby(s)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文